【问题标题】:The C `clock()` function just returns a zeroC `clock()` 函数只返回一个零
【发布时间】:2011-01-09 05:07:39
【问题描述】:

C clock() 函数只返回一个零。我尝试使用不同的类型,但没有任何改进...这是一种高精度测量时间的好方法吗?

#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

平台:Intel 32 位、RedHat Linux、gcc 3.4.6

【问题讨论】:

    标签: c++ c time clock


    【解决方案1】:

    你可能需要

    double get_wall_time(){ struct timeval time; if (gettimeofday(&time,NULL)){ return 0; } return (double)time.tv_sec + (double)time.tv_usec * .000001; } 和使用类似

    double wall0 = get_wall_time(); double cpu0 = get_cpu_time(); for(long int i = 0; i<=10000000;i++){ func1(); } double wall1 = get_wall_time(); double cpu1 = get_cpu_time(); cout << "Wall Time = " << wall1 - wall0 << endl; cout << "CPU Time = " << cpu1 - cpu0 << endl;

    而不是 clock()

    仅作为时钟,仅根据性能计数器计算在 CPU 中花费的时间。 但是您可以使用上述功能获得结果。 只是为了验证您的应用程序使用 time 命令运行它

    点赞time ./a.out

    时间命令的输出:

    real 0m5.987s user 0m0.674s sys 0m0.134s

    自定义函数的输出 Wall Time = 5.98505 CPU Time = 0.8

    【讨论】:

      【解决方案2】:

      调用sleep() 不会占用任何CPU 时间。不过,您应该会看到 一点 的不同。我在这一行更正了您的 printf 类型不匹配错误:

      printf("start = %.20f\nend   = %.20f\n", start, end);
      

      它在我的机器上给出了合理的结果:

      start = 1419
      end   = 1485
      delta = 66
      cpu_time_used  = 0.000066000000000
      CLOCKS_PER_SEC = 1000000
      

      您可以尝试gettimeofday() 来获取运行程序所花费的实时时间。

      【讨论】:

        【解决方案3】:

        clock() 报告使用的 CPU 时间。 sleep() 不使用任何 CPU 时间。所以你的结果可能是完全正确的,只是不是你想要的。

        【讨论】:

        • 你是对的。使用带有一些数学运算的 for 循环,它会返回一个合理的值。谢谢!
        【解决方案4】:
         printf("start = %.20f\nend   = %.20f\n", start, end);
        

        应该是:

         printf("start = %d\nend   = %d\n", start, end);
        

        【讨论】:

          【解决方案5】:

          man clock。它不会返回您认为的内容。还有man gettimeofday - 这更有可能是你想要的。

          【讨论】:

          • gettimeofday 已过时。使用clock_gettime()
          【解决方案6】:

          clock_t 是整数类型。你不能用 %f 打印出来。请参阅Fred's answer 了解为什么差异为 0。

          【讨论】:

          • clock_t 需要是算术类型,可以是整数或浮点类型。因此,您可以使用%f 将其打印出来,但为了安全起见(呃),这些值应该在printf() 调用中转换为(double),这样无论它是整数还是整数,它都能正常工作浮动类型。
          • @Michael,你见过浮动类型的实现吗?我想如果是这样的话,struct tms 会非常混乱。
          • 我无法将你指向一个特定的实例(但话又说回来,除非我看,否则我不知道 Windows 或其他任何使用整数类型的东西)。我知道有几个平台对time_t 使用浮动类型——我不知道这是否意味着它们更有可能对clock_t 执行类似操作。 clock_t 的类型应该对 struct tm 完全没有影响。
          • 我并不是说它很常见或有意义(我实际上不知道它是否曾经使用过),而是那些比我更了解可能要求的人(标准委员会成员)允许 clock_t 为浮动类型。 FWIW,我还查看了 Plauger 的标准 C 库书和 Harbison & Steele,他们都提到 ANSI/ISO C 的 clock_t 可以是浮动的。
          • 著名的例子是爱国者导弹系统。它的时钟使用浮点。因此,当系统运行较长时间时,其绝对精度会下降,例如在海湾战争中。这导致了随后的制导准确性损失、拦截失败和多人死亡。所以,是的,当保罗·汤布林说“事情会一团糟”时,他说得再正确不过了
          猜你喜欢
          • 1970-01-01
          • 2014-01-03
          • 2020-11-20
          • 2011-11-05
          • 2021-11-26
          • 2021-04-15
          • 1970-01-01
          • 2012-04-01
          相关资源
          最近更新 更多