问题描述
我有以下问题:我有来衡量一个程序需要执行的时间。该计划的一个标量版则可以与下面的code,但使用OpenMP时,它的工作原理我的电脑上,但不是在资源,我应该使用。
事实上:
- 标程序RT 34S
- OpenMP程序RT 787-9
这就是我的电脑(一切工作)-compiled与Visual Studio
在我的ressource必须使用(我认为的Linux,用gcc编译):
- 标程序RT 787-9
- OpenMP程序RT 787-9(但文字立即弹出之后,所以它应该是0-1s)
我客串是,它增加了所有的蜱,这是大约相同数量和单核心的节拍率devides他们。我的问题是如何解决这个问题,如果有更好的方式来观看C上的控制台的时间。
clock_t表示启动,停止;
双T = 0.0;
断言((开始=时钟())= -1!);
... code运行
T =(双)(停止 - 起动)/ CLOCKS_PER_SEC;
的printf(运行时间:%F \\ N,T);
要增加马克的回答是:不要使用时钟()
时钟()
是从旧电脑时代,谁是实际执行一个可怕的误区有很大的不同,从平台到平台。看哪:
- 在Linux,* BSD,达尔文(OS X) - 和其他可能的4.3BSD的后裔 -
时钟()
返回处理器时间(不是挂钟!调用进程使用的时间),即每个线程的处理器时间的总和; - 调用进程及其所有终止子进程为其
等
,系统
或函数,pclose
被执行死刑; - HP-UX不甚至似乎实施
时钟()
; - 在Windows
时钟()
返回挂钟时间(不是处理器时间)。
和其他可能的SysV的后裔 - -
时钟()
返回处理器时间(再次没有挂钟时间)被用在IRIX,AIX的Solaris 在上面的描述中的的处理器时间的通常意味着用户和系统时间的总和。这可能是小于壁时钟(实际)时间,例如如果在进程休眠或等待文件IO或网络传输,或者它可能是比挂钟时间以上,例如当处理具有多个线程,积极地使用CPU
不要使用时钟()
。使用 omp_get_wtime()
- 它存在于所有平台,由OpenMP的支持,并且总是返回挂钟时间
I got the following problem: I have to measure the time a program needs to be executed. A scalar version of the program works fine with the code below, but when using OpenMP, it works on my PC, but not on the resource I am supposed to use.In fact:
- scalar program rt 34s
- openmp program rt 9s
thats my pc (everything working) -compiled with visual studiothe ressource I have to use (I think Linux, compiled with gcc):
- scalar program rt 9s
- openmp program rt 9s (but the text pops immediately afterwards up, so it should be 0-1s)
my gues is, that it adds all ticks, which is about the same amount and devides them by the tick rate of a single core. My question is how to solve this, and if there is a better way to watch the time in the console on c.
clock_t start, stop;
double t = 0.0;
assert((start = clock()) != -1);
... code running
t = (double)(stop - start) / CLOCKS_PER_SEC;
printf("Run time: %f\n", t);
To augment Mark's answer: DO NOT USE clock()
clock()
is an awful misunderstanding from the old computer era, who's actual implementation differs greatly from platform to platform. Behold:
- on Linux, *BSD, Darwin (OS X) -- and possibly other 4.3BSD descendants --
clock()
returns the processor time (not the wall-clock time!) used by the calling process, i.e. the sum of each thread's processor time; - on IRIX, AIX, Solaris -- and possibly other SysV descendants --
clock()
returns the processor time (again not the wall-clock time) used by the calling process AND all its terminated child processes for whichwait
,system
orpclose
was executed; - HP-UX doesn't even seem to implement
clock()
; - on Windows
clock()
returns the wall-clock time (not the processor time).
In the descriptions above processor time usually means the sum of user and system time. This could be less than the wall-clock (real) time, e.g. if the process sleeps or waits for file IO or network transfers, or it could be more than the wall-clock time, e.g. when the process has more than one thread, actively using the CPU.
Never use clock()
. Use omp_get_wtime()
- it exists on all platforms, supported by OpenMP, and always returns the wall-clock time.
这篇关于计算时间的实时运行时的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!