【问题标题】:Measureing execution time with C in Windows在 Windows 中使用 C 测量执行时间
【发布时间】: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 &lt; 5; i++) sqrt(i); 所需的时间来测试该方法并检查它。执行时间几乎为 0 的原因之一是因为“Hello world”是硬编码的文字,从文件的 .text 部分获取它并不需要很长时间。
  • 您可能会遇到Microsoft Minute

标签: c windows time


【解决方案1】:

%E 需要浮点数(floatdouble)。您正在发送整数值

除以确保数字是浮动的

(end-begin+0.0)/CLOCKS_PER_SEC);

【讨论】:

    猜你喜欢
    • 2013-03-21
    • 2011-04-23
    • 1970-01-01
    • 2022-01-03
    • 2023-03-09
    • 2015-07-25
    相关资源
    最近更新 更多