的C'时钟()`函数只是返回一个零 [英] The C `clock()` function just returns a zero
本文介绍了的C'时钟()`函数只是返回一个零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
的C 时钟()
函数只是返回我一个零。我试着用不同的类型,有没有改善?这是衡量,用良好的precision的好办法?
的#include<&time.h中GT;
#包括LT&;&stdio.h中GT;诠释的main()
{
clock_t表示起点,终点;
双cpu_time_used; 个char [32]; 开始=时钟(); 的printf(\\ nSleeping数秒\\ n \\ n);
睡眠(3); 结束=时钟(); cpu_time_used =((双)(结束 - 开始))/((双)CLOCKS_PER_SEC); 的printf(开始=%.20f \\ NEND =%.20f \\ n,开始,结束);
的printf(增量=%.20f \\ n,((双)(结束 - 开始)));
的printf(cpu_time_used =%.15f \\ n,cpu_time_used);
的printf(CLOCKS_PER_SEC =%我的\\ n \\ n,CLOCKS_PER_SEC); 返回0;
}
睡觉数秒开始= 0.00000000000000000000
结束= 0.00000000000000000000
三角洲= 0.00000000000000000000
cpu_time_used = 0.000000000000000
CLOCKS_PER_SEC = 1000000
块引用>平台:英特尔32位,RedHat Linux上,GCC 3.4.6
解决方案
时钟()
报告所使用的CPU时间。睡眠()
不使用任何CPU时间。所以,你的结果可能是完全正确的,只是不是你想要的。The C
clock()
function just returns me a zero. I tried using different types, with no improvement... Is this a good way to measure time with good precision?#include <time.h> #include <stdio.h> int main() { clock_t start, end; double cpu_time_used; char s[32]; start = clock(); printf("\nSleeping 3 seconds...\n\n"); sleep(3); end = clock(); cpu_time_used = ((double)(end - start)) / ((double)CLOCKS_PER_SEC); printf("start = %.20f\nend = %.20f\n", start, end); printf("delta = %.20f\n", ((double) (end - start))); printf("cpu_time_used = %.15f\n", cpu_time_used); printf("CLOCKS_PER_SEC = %i\n\n", CLOCKS_PER_SEC); return 0; }
Sleeping 3 seconds... start = 0.00000000000000000000 end = 0.00000000000000000000 delta = 0.00000000000000000000 cpu_time_used = 0.000000000000000 CLOCKS_PER_SEC = 1000000
Platform: Intel 32 bit, RedHat Linux, gcc 3.4.6
解决方案
clock()
reports CPU time used.sleep()
doesn't use any CPU time. So your result is probably exactly correct, just not what you want.这篇关于的C'时钟()`函数只是返回一个零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文