GCC time.h问题。 [英] GCC time.h problem.
问题描述
您好!
我是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?
推荐答案
Jaideep写道:
Jaideep wrote:
无论如何我写了这段小代码来计算过程时间。这些代码在windows编译器中运行良好但在GCC中会产生不稳定的输出。可能是什么问题?
代码 - >
......
#include< time.h>
....
void main(){
clock_t start,end;
start = clock();
//进程();
end = clock();
printf(时间是:%f \ n,(结束 - 开始)/ CLK_TCK);
}
有没有其他方法可以做同样的事情?
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?
Jaideep写道:
Jaideep wrote:
你好!
我是GCC的新手。我已经有4到5年的Windows编程经验了。我发现GCC比turbo或microsoft
编译器慢得多。不管我写的这个小计算进程时间的代码片段。代码在windows编译器中工作正常但在GCC中输出不稳定。可能是什么问题?
代码 - >
...
#include< time.h>
....
void main(){
clock_t start,end;
start = clock();
// process();
end = clock();
printf("时间:%f \ n \\ 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?
不稳定输出的可能原因(你应该
已经解释了你的意思):
- 你调用了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
es ***** @ acm-dot-org.inva 盖子
Jaideep写道:
Jaideep wrote:
你好!
我是GCC的新手。我已经有4到5年的窗户经验
'' C''编程。我发现GCC比turbo或微软慢得多b
我喜欢''C'中的引号。 LoL
compilers.Anyway我写了这段小代码来计算过程时间。代码在windows编译器中工作正常但在GCC中输出不稳定。可能是什么问题?
代码 - >
不是真的,因为它不会编译cut'''''pasted ......
...
#include< ; time.h>
....
void main(){
这是你的问题。这样做之后,它不再是C,你可能会看到奇怪的东西。函数`main`必须返回一个`int`。
clock_t start,end;
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
I love the quotes in ''C''. LoL
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-->
Not really, as it won''t compile cut''n''pasted...
...
#include <time.h>
....
void main() {
This is your problem. After doing this, it''s no longer C, and you may
observe weird stuff. Function `main` has to return an `int`.
clock_t start, end;
start = clock();
//process();
end = clock();
printf("The time was: %f\n", (end - start) / CLK_TCK);
标准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问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!