The C `clock()` function just returns a zero(C `clock()` 函数只返回一个零)
问题描述
C clock()
函数只返回零.我尝试使用不同的类型,但没有任何改进......这是一种以高精度测量时间的好方法吗?
#include #include int main(){clock_t 开始,结束;双 cpu_time_used;字符 [32];开始 = 时钟();printf("
睡眠 3 秒...
");睡眠(3);结束 = 时钟();cpu_time_used = ((double)(end - start))/((double)CLOCKS_PER_SEC);printf("start = %.20f
end = %.20f
", start, end);printf("delta = %.20f
", ((double) (end - start)));printf("cpu_time_used = %.15f
", cpu_time_used);printf("CLOCKS_PER_SEC = %i
", CLOCKS_PER_SEC);返回0;}
<块引用>
睡眠3秒...开始 = 0.00000000000000000000结束 = 0.00000000000000000000增量 = 0.00000000000000000000cpu_time_used = 0.000000000000000CLOCKS_PER_SEC = 1000000
平台:Intel 32 位,RedHat Linux,gcc 3.4.6
clock()
报告 CPU 使用时间.sleep()
不使用任何 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("
Sleeping 3 seconds...
");
sleep(3);
end = clock();
cpu_time_used = ((double)(end - start)) / ((double)CLOCKS_PER_SEC);
printf("start = %.20f
end = %.20f
", start, end);
printf("delta = %.20f
", ((double) (end - start)));
printf("cpu_time_used = %.15f
", cpu_time_used);
printf("CLOCKS_PER_SEC = %i
", 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 `clock()` 函数只返回一个零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C `clock()` 函数只返回一个零


基础教程推荐
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 常量变量在标题中不起作用 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01