why slow 5 times when tic or toc and expression in one line?
12 次查看(过去 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
tic;
for k = 1:9e7;a = plus(1,1);end;
toc
Elapsed time is 0.974705 seconds.
Elapsed time is 0.187138 seconds.
2 个评论
Stephen23
2015-3-20
On MATLAB 2010b:
% 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
prints:
Elapsed time is 0.818340 seconds.
Elapsed time is 0.813082 seconds.
Michael Haderlein
2015-3-20
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.
采纳的回答
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
%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 个评论
Matz Johansson Bergström
2015-3-20
编辑:Matz Johansson Bergström
2015-3-20
BTW, if you turn off JIT ( feature accel off) the loops will both take ~20 s, so it obviously does something right.
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!