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

조회 수: 11 (최근 30일)
Binbin Qi 2015년 3월 20일
답변: Binbin Qi 2015년 3월 20일
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.
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이전 댓글 -1개 표시이전 댓글 -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개)

Binbin Qi 2015년 3월 20일
Thank you, I guess the reason is JIT.Now, I confirmed

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by