why slow 5 times when tic or toc and expression in one line?

18 views (last 30 days)
when tic or toc and the main expression are in one line, it will be slow 5 times than not in a line
1: tic or toc and the expression in one line
tic;for k = 1:9e7;a = plus(1,1);end;toc
2. not in one line
tic;
for k = 1:9e7;a = plus(1,1);end;
toc
Elapsed time is 0.974705 seconds.
Elapsed time is 0.187138 seconds.
  2 Comments
Michael Haderlein
Michael Haderlein on 20 Mar 2015
Same code, output:
Elapsed time is 28.378463 seconds.
Elapsed time is 27.739867 seconds.
Comparison of the values indicates no difference between the two cases. Absolute values indicate that I want a new computer.

Sign in to comment.

Accepted Answer

Matz Johansson Bergström
Matz Johansson Bergström on 20 Mar 2015
Edited: Matz Johansson Bergström on 20 Mar 2015
Funny enough I also get horrible timings. I am using Matlab 2014a and I get:
Elapsed time is 1.053381 seconds.
Elapsed time is 0.216281 seconds.
I will try to explain what I think is going on.
Matlab is interpreted and because of this it has to accelerate loops using JIT (Just-In-Time) compilation. The compiler is not perfect, because it is done rather quickly (it is done "just in time"). It usually manages to convert loops to vectorized code, which is very fast and more comparable to the speed you would get from a compiled language. JIT manages to successfully accelerate the inner loops of a nested loop situation pretty well.
In this case we have only one loop, so it should be very fast. By simply wrapping the for and end statement with tic and toc, we get slow code. This should be very simple for JIT to realize what is going on.
Let us just check a couple of things. First I remove the call to plus, this is not important. Now, let's see
%0.83 s
tic; for k = 1:9e7; end; toc
%0.19 s
tic
for k = 1:9e7; end;
toc
Let's try to arrange the tic and toc a bit
%0.98 s
tic;for k = 1:9e7;
end; toc
Ok, so we agree that it seems that if you place a tic before a for AND a toc right after an end you will not get a accelerated code. You have to move both tic and toc out of the way for JIT to realize what you are really doing.
My guess is that at some point between Matlab 2010 and Matlab 2014, the JIT accelerator rules were changed to allow for this weird behavior.
Also please note that I am not doing anything in the loop, still the code is slow, this confirms that JIT is not that smart ;-)
  1 Comment
Matz Johansson Bergström
Matz Johansson Bergström on 20 Mar 2015
Edited: Matz Johansson Bergström on 20 Mar 2015
BTW, if you turn off JIT ( feature accel off) the loops will both take ~20 s, so it obviously does something right.

Sign in to comment.

More Answers (1)

Binbin Qi
Binbin Qi on 20 Mar 2015
Thank you, I guess the reason is JIT.Now, I confirmed

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!