why slow 5 times when tic or toc and expression in one line?
조회 수: 11 (최근 30일)
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
for k = 1:9e7;a = plus(1,1);end;
Elapsed time is 0.974705 seconds.
Elapsed time is 0.187138 seconds.
Matz Johansson Bergström 2015년 3월 20일
편집: Matz Johansson Bergström 2015년 3월 20일
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
tic; for k = 1:9e7; end; toc
for k = 1:9e7; end;
Let's try to arrange the tic and toc a bit
tic;for k = 1:9e7;
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 ;-)