在 C++ OpenMP 代码中测量执行时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10874214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Measure execution time in C++ OpenMP code
提问by Benny
I am running a .cpp code (i) in sequential style and (ii) using OpenMP statements. I am trying to see the time difference. For calculating time, I use this:
我正在运行 .cpp 代码 (i) 以顺序样式和 (ii) 使用 OpenMP 语句。我正在尝试查看时差。为了计算时间,我使用这个:
#include <time.h>
.....
main()
{
clock_t start, finish;
start = clock();
.
.
.
finish = clock();
processing time = (double(finish-start)/CLOCKS_PER_SEC);
}
The time is pretty accurate in sequential (above) run of the code. It takes about 8 seconds to run this. When I insert OpenMP statements in the code and thereafter calculate the time I get a reduction in time, but the time displayed is about 8-9 seconds on the console, when actually its just 3-4 seconds in real time!
时间在代码的顺序(上面)运行中非常准确。运行这个大约需要 8 秒。当我在代码中插入 OpenMP 语句并随后计算时间时,我得到的时间减少了,但是在控制台上显示的时间大约是 8-9 秒,而实际上它的实时时间只有 3-4 秒!
Here is how my code looks abstractly:
这是我的代码的抽象外观:
#include <time.h>
.....
main()
{
clock_t start, finish;
start = clock();
.
.
#pragma omp parallel for
for( ... )
for( ... )
for (...)
{
...;
}
.
.
finish = clock();
processing time = (double(finish-start)/CLOCKS_PER_SEC);
}
When I run the above code, I get the reduction in time but the time displayed is not accurate in terms of real time. It seems to me as though the clock () function is calculating each thread's individual time and adding up them up and displaying them.
当我运行上面的代码时,我得到了时间的减少,但显示的时间在实时方面并不准确。在我看来,clock () 函数似乎正在计算每个线程的单独时间并将它们相加并显示它们。
Can someone tell the reason for this or suggest me any other timing function to use to measure the time in OpenMP programs?
有人可以说出原因或建议我使用任何其他计时函数来测量 OpenMP 程序中的时间吗?
Thanks.
谢谢。
采纳答案by sehe
I've seen clock() reporting CPU time, instead of real time.
我见过 clock() 报告 CPU 时间,而不是实时。
You could use
你可以用
struct timeval start, end;
gettimeofday(&start, NULL);
// benchmark code
gettimeofday(&end, NULL);
delta = ((end.tv_sec - start.tv_sec) * 1000000u +
end.tv_usec - start.tv_usec) / 1.e6;
To time things instead
为事情计时
回答by Hristo Iliev
It seems to me as though the clock () function is calculating each thread's individual time and adding up them up and displaying them.
在我看来,clock () 函数似乎正在计算每个线程的单独时间并将它们相加并显示它们。
This is exactlywhat clock()
does - it measures the CPU time used by the process, which at least on Linux and Mac OS X means the cumulative CPU time of all threads that have ever existed in the process since it was started.
这究竟是什么clock()
呢-它通过测量过程中,至少在Linux和Mac OS X是指已经自启动以来在这个过程中曾经存在过的所有线程的CPU累计时使用的CPU时间。
Real-clock (a.k.a. wall-clock) timing of OpenMP applications should be done using the high resolution OpenMP timer call omp_get_wtime()
which returns a double
value of the number of seconds since an arbitrary point in the past. It is a portable function, e.g. exists in both Unix and Windows OpenMP run-times, unlike gettimeofday()
which is Unix-only.
OpenMP 应用程序的实时时钟(又名挂钟)计时应该使用高分辨率 OpenMP 计时器调用来完成,该调用omp_get_wtime()
返回double
自过去任意点以来的秒数值。它是一个可移植的功能,例如在 Unix 和 Windows OpenMP 运行时中都存在,这与gettimeofday()
Unix-only 不同。
回答by Ben Voigt
Well yes, that's what clock()
is supposed to do, tell you how much processor time the program used.
嗯,是的,这clock()
就是应该做的,告诉你程序使用了多少处理器时间。
If you want to find elapsed real time, instead of CPU time, use a function that returns wall clock time, such as gettimeofday()
.
如果要查找经过的实时时间,而不是 CPU 时间,请使用返回挂钟时间的函数,例如gettimeofday()
.
回答by Erdinc Ay
#include "ctime"
std::time_t start, end;
long delta = 0;
start = std::time(NULL);
// do your code here
end = std::time(NULL);
delta = end - start;
// output delta