gpt4 book ai didi

c++ - 测量子进程的时间

转载 作者:搜寻专家 更新时间:2023-10-31 01:37:25 25 4
gpt4 key购买 nike

我想测量子进程的时间

#include <time.h>

int main() {
...
time t begin, end, diff;
...
//fork etc in here
time(&begin);
...
//some things
...
time(&end);
return 0;
}

我现在有 2 个时间戳,有没有办法将其格式化为子进程的运行时间为小时:分钟:秒?

我试过了

diff = end - begin;

但我得到了一个巨大的数字。

(对不起,只有一部分代码,但它在另一台电脑上。)

最佳答案

您可以使用 difftime 计算差异:

double diff_in_seconds = difftime(end, begin);

或者,为了更精确,使用 C++11 chrono 之一单调时钟,例如 std::steady_clock :

auto start = std::chrono::steady_clock::now();
// some things
auto end = std::chrono::steady_clock::now();
double time_in_seconds = std::chrono::duration_cast<double>(end - start).count();

另见 this answer有关为什么应该使用单调时钟的详细信息。

关于c++ - 测量子进程的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34159228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com