【发布时间】:2016-06-15 00:42:42
【问题描述】:
我想测量代码和平的执行时间。
我正在使用 clock() 函数。
这里是例子:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(void) {
clock_t begin=0, end=0;
begin = clock();
printf("hello world!!\n");
end = clock();
printf("Begin %E\n",begin);
printf("End %E\n",end);
printf("Clocks %E\n",end-begin);
printf("Clocks per second %E\n", CLOCKS_PER_SEC);
printf("Time %E\n", (end-begin)/CLOCKS_PER_SEC);
return 0;
}
这里是输出:
hello world!!
Begin 2.529616E-320
End 2.535545E-320
Clocks 5.928788E-323
Clocks per second 4.940656E-318
Time 0.000000E+00
时间是0!
我做错了什么?
输出是否正确?
非常感谢
【问题讨论】:
-
小于皮秒。我认为这在 Windows 中是不可能的......
-
测量单个 printf 语句的执行时间是没有意义的。
-
通过测量计算
for(int i = 1; i < 5; i++) sqrt(i);所需的时间来测试该方法并检查它。执行时间几乎为 0 的原因之一是因为“Hello world”是硬编码的文字,从文件的.text部分获取它并不需要很长时间。 -
您可能会遇到Microsoft Minute。