Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB's tic-toc & C's clock discrepancy

Tags:

c

time

matlab

mex

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.

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:

  1. The execution time for the whole function is lower than the execution time for a part of the code. Hence, either MATLAB's or C's measurements are strongly inaccurate.
  2. The execution times measured in the C code are very scattered and exhibit very high st. deviation (coeff. of variation 44%, compared to just 13% for tic-toc).

What is going on with these timers?

like image 695
Pantelis Sopasakis Avatar asked Jan 31 '13 22:01

Pantelis Sopasakis


People also ask

What is tic and Toc in MATLAB?

The tic function records the current time, and the toc function uses the recorded value to calculate the elapsed time. example. timerVal = tic stores the current time in timerVal so that you can pass it explicitly to the toc function.

How do I record tic Toc in MATLAB?

Use a pair of tic and toc calls to report the total time required for element-by-element matrix multiplication; use another pair to report the total runtime of your program. The variable tMul includes the total time spent on multiplication.

How accurate is tic Toc in MATLAB?

We can read that "tic and toc [offers] the highest accuracy and most predictable behavior". I think it is valid statement. The drop of performance observed here is not due to a bad measure of elapsed time, and not related either to the use of imshow or drawnow functions.

How do I record time in MATLAB?

To measure the time required to run a function, use the timeit function. The timeit function calls the specified function multiple times, and returns the median of the measurements. It takes a handle to the function to be measured and returns the typical execution time, in seconds.


1 Answers

You're comparing apples to oranges.

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 and toc let you measure real elapsed time.

Now look at the clock function http://linux.die.net/man/3/clock.

In particular,

The clock() function returns an approximation of processor time used by the program.

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 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.

like image 170
thang Avatar answered Oct 05 '22 15:10

thang