我想测量子进程的时间
#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;
但是我得到了很多。
(仅对部分代码感到抱歉,但它在另一台PC上。)
您可以使用以下方法计算差异difftime
:
double diff_in_seconds = difftime(end, begin);
或者,为了获得更高的精度,请使用C ++ 11计时单调时钟之一,例如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();
另请参阅此答案以了解为什么您应该使用单调时钟的详细信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句