本文介绍了GCC time.h问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!

我是GCC的新手。我已经拥有4-5年的Windows经验

''C''编程。我找到了GCC比turbo或microsoft慢得多

compilers.Anyway我写了这段小代码来计算过程

time。该代码在windows编译器中工作正常但输出不稳定
。可能是什么问题?


代码 - >

....

#include< time.h>

.....

void main(){


clock_t start ,结束;


start = clock();

// process();


end = clock ();

printf(时间是:%f \ n,(结束 - 开始)/ CLK_TCK);

}

有没有其他方法可以做同样的事情?

Hello!
I am new to GCC.I''ve abt 4-5 yrs of experience in windows
''C'' programming.I find GCC much slower than turbo or microsoft
compilers.Anyway I wrote this small piece of code to calculate process
time .The code works fine in windows compilers but gives erratic output
in GCC.What could be the problem?

code-->
....
#include <time.h>
.....
void main() {

clock_t start, end;

start = clock();
//process();

end = clock();
printf("The time was: %f\n", (end - start) / CLK_TCK);
}
Is there any other way of doing the same?

推荐答案



clc处理标准C.您是否尝试过这个标准C版本?

例如将CLK_TCK替换为(双)CLOCKS_PER_SEC

是什么让你如此确定所有windows编译器?将使用非标准代码使用相同的



c.l.c deals with standard C. Did you try a standard C version of this?
e.g. replace CLK_TCK by (double)CLOCKS_PER_SEC
What makes you so sure that all "windows compilers" will work the same
with non-standard code?





不稳定输出的可能原因(你应该

已经解释了你的意思):


- 你调用了printf()函数但是没有

#include< stdio.h>。因此,您的程序具有

未定义的行为。


- 您将main()函数声明为void而不是

of 'INT ''。因此,您的程序未定义

行为。


- 您使用了%f打印结果,但是%f需要

相应的双倍值。你不知道

类型的表达式`(end-start)/ CLK_TICK''

因为每个实现都可以自己创建

选择,所以在一个实现中,这个表达式

产生的东西不是'double''你的程序

表现出不确定的行为。


- 在`clock_t''和`CLK_TICK''

碰巧是整数的实现中,你会计算零持续时间

任何小于的间隔一秒钟。


如果你原谅我这么说,你的经历了4-5岁经历

似乎没有教你很多关于C.也许你已经使用某种C-ish方言而不是C本身,

并且已经收集了大量的错误信息坏习惯。


-

Eric Sosman
盖子





标准C未提及CLK_TCK。另外,你不包括stdio.h,

所以`printf`是未知的。然后,`clock_t`是整数类型,%f是

几乎没有正确的打印方式。



Standard C does not mention CLK_TCK. Also, you don''t include stdio.h,
so `printf` is unknown. And then, `clock_t` is integer type, and %f is
hardly the correct way of printing it.


这篇关于GCC time.h问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:01