我有一个C程序,目的是在几个处理器上并行运行。我需要能够记录执行时间(可以从1秒到几分钟不等)。我已经搜索了答案,但它们似乎都建议使用clock()函数,然后涉及计算程序所用的时钟数除以Clocks_per_second值。

我不确定Clocks_per_second值是如何计算的?

在Java中,我只是在执行前后以毫秒为单位获取当前时间。

C语言中也有类似的东西吗?我看了一下,但我似乎找不到比第二次分辨率更好的方法。

我也知道一个分析器将是一个选项,但我希望自己实现一个定时器。

谢谢


当前回答

(如果您的系统管理员更改了系统时间,或者您的时区有不同的冬季和夏季时间,这里就没有所有的答案。因此…)

在linux上使用:clock_gettime(clock_单调ic_raw, &time_variable); 如果系统管理员改变了时间,或者你生活在一个冬季和夏季不同的国家,等等,它不会受到影响。

#include <stdio.h>
#include <time.h>

#include <unistd.h> /* for sleep() */

int main() {
    struct timespec begin, end;
    clock_gettime(CLOCK_MONOTONIC_RAW, &begin);

    sleep(1);      // waste some time

    clock_gettime(CLOCK_MONOTONIC_RAW, &end);

    printf ("Total time = %f seconds\n",
            (end.tv_nsec - begin.tv_nsec) / 1000000000.0 +
            (end.tv_sec  - begin.tv_sec));

}

Man clock_gettime声明:

CLOCK_MONOTONIC
              Clock  that  cannot  be set and represents monotonic time since some unspecified starting point.  This clock is not affected by discontinuous jumps in the system time
              (e.g., if the system administrator manually changes the clock), but is affected by the incremental adjustments performed by adjtime(3) and NTP.

其他回答

(如果您的系统管理员更改了系统时间,或者您的时区有不同的冬季和夏季时间,这里就没有所有的答案。因此…)

在linux上使用:clock_gettime(clock_单调ic_raw, &time_variable); 如果系统管理员改变了时间,或者你生活在一个冬季和夏季不同的国家,等等,它不会受到影响。

#include <stdio.h>
#include <time.h>

#include <unistd.h> /* for sleep() */

int main() {
    struct timespec begin, end;
    clock_gettime(CLOCK_MONOTONIC_RAW, &begin);

    sleep(1);      // waste some time

    clock_gettime(CLOCK_MONOTONIC_RAW, &end);

    printf ("Total time = %f seconds\n",
            (end.tv_nsec - begin.tv_nsec) / 1000000000.0 +
            (end.tv_sec  - begin.tv_sec));

}

Man clock_gettime声明:

CLOCK_MONOTONIC
              Clock  that  cannot  be set and represents monotonic time since some unspecified starting point.  This clock is not affected by discontinuous jumps in the system time
              (e.g., if the system administrator manually changes the clock), but is affected by the incremental adjustments performed by adjtime(3) and NTP.

原味的C:

#include <time.h>
#include <stdio.h>

int main()
{
    clock_t tic = clock();

    my_expensive_function_which_can_spawn_threads();

    clock_t toc = clock();

    printf("Elapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);

    return 0;
}

Thomas Pornin的回答是:

#define TICK(X) clock_t X = clock()
#define TOCK(X) printf("time %s: %g sec.\n", (#X), (double)(clock() - (X)) / CLOCKS_PER_SEC)

像这样使用它:

TICK(TIME_A);
functionA();
TOCK(TIME_A);

TICK(TIME_B);
functionB();
TOCK(TIME_B);

输出:

time TIME_A: 0.001652 sec.
time TIME_B: 0.004028 sec.

我发现通常的clock(),这里每个人都推荐,由于某种原因在一次运行中严重偏离,即使对于静态代码也没有任何副作用,如绘制到屏幕或读取文件。这可能是因为CPU改变了功耗模式,操作系统给出了不同的优先级,等等…

因此,每次使用clock()都可靠地获得相同结果的唯一方法是在循环中多次运行测量的代码(持续几分钟),并采取预防措施防止编译器对其进行优化:现代编译器可以预先计算在循环中运行的代码而不会产生副作用,并将其移出循环。例如,每次迭代都使用随机输入。

在一个数组中收集了足够多的样本后,对数组进行排序,并取中间的元素,称为中位数。中位数比平均值好,因为它排除了极端偏差,比如反病毒病毒占用了所有CPU或操作系统进行了一些更新。

这里有一个简单的实用程序来测量C/ c++代码的执行性能,平均值接近中值:https://github.com/saniv/gauge

我自己仍然在寻找一种更健壮、更快的方法来度量代码。人们可能会尝试在没有任何操作系统的情况下在受控条件下运行代码,但这会产生不切实际的结果,因为在现实中操作系统确实涉及到。

x86有这些硬件性能计数器,包括实际执行的指令数量,但是如果没有操作系统的帮助,它们很难访问,很难解释,并且有自己的问题(http://archive.gamedev.net/archive/reference/articles/article213.html)。尽管如此,他们仍然可以帮助调查瓶颈的性质(数据访问或对数据的实际计算)。

如果您正在使用Unix shell运行,则可以使用time命令。

$ time ./a.out

假设a.out作为可执行文件将为你提供运行这个程序所需的时间