C语言 C:在多线程程序中使用clock()测量时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2962785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:34:10  来源:igfitidea点击:

C: using clock() to measure time in multi-threaded programs

c

提问by Suugaku

I've always used clock() to measure how much time my application took from start to finish, as;

我一直使用 clock() 来测量我的应用程序从开始到结束所花费的时间,例如;

int main(int argc, char *argv[]) {
  const clock_t START = clock();

  // ...

  const double T_ELAPSED = (double)(clock() - START) / CLOCKS_PER_SEC;
}

Since I've started using POSIX threads this seem to fail. It looks like clock() increases N times faster with N threads. As I don't know how many threads are going to be running simultaneously, this approach fails. So how can I measure how much time has passed ?

由于我已经开始使用 POSIX 线程,这似乎失败了。看起来 clock() 使用 N 个线程增加 N 倍。由于我不知道将同时运行多少个线程,因此这种方法失败了。那么我如何衡量已经过去了多少时间?

回答by caf

clock()measure the CPU time used by your process, not the wall-clock time. When you have multiple threads running simultaneously, you can obviously burn through CPU time much faster.

clock()测量进程使用的 CPU 时间,而不是挂钟时间。当您同时运行多个线程时,显然可以更快地消耗 CPU 时间。

If you want to know the wall-clock execution time, you need to use an appropriate function. The only one in ANSI C is time(), which typically only has 1 second resolution.

如果您想知道挂钟执行时间,则需要使用适当的函数。ANSI C 中唯一的一个是time(),通常只有 1 秒的分辨率。

However, as you've said you're using POSIX, that means you can use clock_gettime(), defined in time.h. The CLOCK_MONOTONICclock in particular is the best to use for this:

但是,正如您所说,您使用的是 POSIX,这意味着您可以使用clock_gettime(), 定义在time.h. 将CLOCK_MONOTONIC在特定的时钟是最好用这样的:

struct timespec start, finish;
double elapsed;

clock_gettime(CLOCK_MONOTONIC, &start);

/* ... */

clock_gettime(CLOCK_MONOTONIC, &finish);

elapsed = (finish.tv_sec - start.tv_sec);
elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;

(Note that I have done the calculation of elapsedcarefully to ensure that precision is not lost when timing very short intervals).

(请注意,我已经elapsed仔细地进行了计算,以确保在计时非常短的间隔时不会丢失精度)。

If your OS doesn't provide CLOCK_MONOTONIC(which you can check at runtime with sysconf(_SC_MONOTONIC_CLOCK)), then you can use CLOCK_REALTIMEas a fallback - but note that the latter has the disadvantage that it will generate incorrect results if the system time is changed while your process is running.

如果您的操作系统不提供CLOCK_MONOTONIC(您可以在运行时检查sysconf(_SC_MONOTONIC_CLOCK)),那么您可以将其CLOCK_REALTIME用作后备 - 但请注意,后者的缺点是如果在您的进程运行时更改系统时间,它将产生不正确的结果.

回答by Manfre

What timing resolution do you need? You could use time() from time.h for second resolution. If you need higher resolution, then you could use something more system specific. See Timer function to provide time in nano seconds using C++

您需要什么时序分辨率?您可以使用 time.h 中的 time() 作为第二个分辨率。如果您需要更高的分辨率,那么您可以使用更特定于系统的东西。请参阅Timer 函数以使用 C++ 以纳秒为单位提供时间