我想测量 CPU 时间,而不是线程上的经过时间。例如,如果一个线程正在等待或休眠,它不应该计入 CPU 时间,因为该线程不处于可运行状态。所以我得到了以下链接来获取 CPU 时间。但是,它似乎是根据我下面的测试来捕获经过的时间(我希望 cpu_time_used 应该接近 0,但实际上是 2)。我错过了什么?
https://www.gnu.org/software/libc/manual/html_node/CPU-Time.html
#include <time.h>
clock_t start, end;
double cpu_time_used;
start = clock();
std::this_thread::sleep_for (std::chrono::seconds(2));
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;