问题描述
我编写了一些C代码,在使用MEX进行编译后,我将其称为MATLAB.在C代码内部,我使用以下代码来测量计算 part 的时间:
I have written some C code which I call form MATLAB after I compile it using MEX. Inside the C code, I measure the time of a part of the computation using the following code:
clock_t begin, end;
double time_elapsed;
begin = clock();
/* do stuff... */
end = clock();
time_elapsed = (double) ((double) (end - begin) / (double) CLOCKS_PER_SEC);
经过时间应为执行时间,以秒为单位.
Elapsed time should be the execution time in seconds.
然后我将值time_elapsed
输出到MATLAB(已正确导出;已检查).然后在MATLAB端,我将此函数称为C函数(在使用MEX进行编译之后),并使用tic
和toc
来测量其执行时间.完全荒谬的是,我使用tic和toc计算的时间是0.0011s(500次运行的平均值,st.dev.1.4e-4),而C代码返回的时间是0.037s(平均500次运行,标准偏差0.0016).
I then output the value time_elapsed
to MATLAB (it is properly exported; I checked). Then MATLAB-side I call this C function (after I compile it using MEX) and I measure its execution time using tic
and toc
. What turns out to be a complete absurdity is that the time I compute using tic and toc is 0.0011s (average on 500 runs, st. dev. 1.4e-4) while the time that is returned by the C code is 0.037s (average on 500 runs, st. dev. 0.0016).
在这里您可能会注意到两个非常奇怪的事实:
Here one may notice two very strange facts:
- 整个功能的执行时间比部分代码的执行时间短.因此,无论是MATLAB还是C的测量都是非常不准确的.
- 在C代码中测量的执行时间非常分散,并且显示出很高的st.偏差(变化系数为44%,而tic-toc仅为13%).
这些计时器是怎么回事?
What is going on with these timers?
推荐答案
您正在将苹果与桔子进行比较.
You're comparing apples to oranges.
查看Matlab的文档:
Look at Matlab's documentation:
tic - http://www.mathworks .com/help/matlab/ref/tic.html
toc - http://www.mathworks.com/help/matlab/ref/toc.html
tic 和 toc 可让您测量实际经过的时间.
tic and toc let you measure real elapsed time.
现在查看 clock 函数 http://linux.die .net/man/3/clock .
尤其是
返回的值是到目前为止已使用的CPU时间作为clock_t;到 获得使用的秒数,除以CLOCKS_PER_SEC.如果 使用的处理器时间不可用或其值不能为 表示,该函数返回值(clock_t)-1.
The value returned is the CPU time used so far as a clock_t; to get the number of seconds used, divide by CLOCKS_PER_SEC. If the processor time used is not available or its value cannot be represented, the function returns the value (clock_t) -1.
那么,什么可以解释您的差异:
So what can account for your difference:
- CPU时间(由clock()度量)和实际经过的时间(由tic和toc度量)不相同.因此,您希望CPU时间比经过的时间短吗?也许会.如果您在0.0011秒内以100%的速度驱动10个内核怎么办?这意味着clock()测量值是使用tic和toc测量的10倍.可能,不太可能.
- clock(.)完全不准确,并且与文档一致,它是大约的CPU时间度量!我怀疑它与调度程序的量子大小有关,但是我没有深入研究Linux内核代码进行检查.我也没有检查其他操作系统,但是此人的博客与该理论一致.
- CPU time (measured by clock()) and real elapsed time (measured by tic and toc) are NOT the same. So you would expect that cpu time to be less than elapsed time? Well, maybe. What if within 0.0011s you're driving 10 cores at 100%? That would mean that clock() measurement is 10x that measured with tic and toc. Possible, unlikely.
- clock(.) is grossly inaccurate, and consistent with the documentation, it is an approximate cpu time measurement! I suspect that it is pegged to the scheduler quantum size, but I didn't dig through the Linux kernel code to check. I also didn't check on other OSes, but this dude's blog is consistent with that theory.
那么该怎么做...对于初学者,将苹果与苹果进行比较!接下来,请确保考虑到计时器分辨率.
So what to do... for starters, compare apples to apples! Next, make sure you take into account timer resolution.
这篇关于MATLAB的tic-toc& C的时钟差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!