在C ++中测量时间 [英] Measuring time in C++

查看:116
本文介绍了在C ++中测量时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的c ++代码。

This is my c++ code.

double start_time = time(NULL);
double start_clock = clock();

#pragma omp parallel for private(i) 
for(i=0;i<max_i;i++)
      PROCESS(i);

double end_time = time(NULL);
double end_clock = clock();

printf("%lf second(s)\n", end_time-start_time);
printf("%lf second(s)\n", (end_clock-start_clock)/CLOCKS_PER_SEC);

这是输出。

took 2.000000 second(s)
took 11.410000 second(s)

有没有人知道为什么这些不一致?有没有其他方法测量这个?

Does anyone know why these are not consistent? Is there any other way of measuring this? BTW, 2 seconds seems more reasonable based on the time I'm seeing here.

推荐答案

clock ()函数返回进程自启动以来所使用的 CPU时间,而不是根据实时时钟的绝对时间。

The clock() function returns the amount of CPU time used by your process since it started, not the absolute time according to a real-time clock.

在另一个注释中,你说 CODE_BLOCK 是一个并行循环 - 这意味着在你的情况下,它使用相当于11.41秒的CPU时间2秒的实时(挂钟)时间。显然你正在并行使用大约6个CPU的力量。

In another comment you said that CODE_BLOCK is a parallel loop - which means in your case, it used the equivalent of 11.41 seconds of CPU time in 2 seconds of real ("wall clock") time. Evidently you're using the power of about 6 CPUs in parallel.

这篇关于在C ++中测量时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆