在CPU中花费的时间比实际情况更快

Time spends in CPU faster than in reality

本文关键字:实际情况 时间 CPU      更新时间:2023-10-16

我想知道为什么我的整个应用程序在小于8 seconds的时间内运行,而从clock_gettime获得的时间是19.3468 seconds,这是实际情况的两倍多。问题出在哪里?

clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time_start);
... // many calculations
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time_stop);
double time_diff=(time_stop.tv_sec-time_start.tv_sec+
    (1e-9)*(time_stop.tv_nsec-time_start.tv_nsec);

更新:

我没有明确使用任何OpenMP。

CLOCK_MONOTONIC如果您想测量总运行时间,包括阻止等待IO所花费的时间,则应该使用,但它也将包括程序试图运行时其他进程被调度所导致的速度减慢。