Why tic/toc reports slower times the first five iterations?

3 ビュー (過去 30 日間)
SARA RUIZ MORENO
SARA RUIZ MORENO 2022 年 9 月 13 日
コメント済み: Walter Roberson 2022 年 9 月 13 日
for i = 1:10
tic
a=2*3;
toc
end
Elapsed time is 0.000094 seconds. Elapsed time is 0.000045 seconds. Elapsed time is 0.000075 seconds. Elapsed time is 0.000014 seconds. Elapsed time is 0.000159 seconds. Elapsed time is 0.000003 seconds. Elapsed time is 0.000002 seconds. Elapsed time is 0.000002 seconds. Elapsed time is 0.000001 seconds. Elapsed time is 0.000001 seconds.

回答 (2 件)

Bruno Luong
Bruno Luong 2022 年 9 月 13 日
編集済み: Bruno Luong 2022 年 9 月 13 日
If I remember, the JIT optimizer decides to kick in when the same code is detected by execution engine to run few times (5).
  1 件のコメント
Walter Roberson
Walter Roberson 2022 年 9 月 13 日
Historically, during the time period that Mathworks used the phrase JIT, then a limited set of operations were supported, and the optimization did not kick in the first pass.
However now that they use the term "execution engine", supposedly all execution goes through it, and supposedly it starts immediately, with the analysis at the time the code is parsed. Supposedly.
That said, in a fair number of tests, I have seen the second and third iterations take much of time.

サインインしてコメントする。


Image Analyst
Image Analyst 2022 年 9 月 13 日
There is some randomness to it. Your computer does other things you know. Who knows what else it may have been doing while that code was running? When I run it I get different numbers every time, though the the first one is always the longest and they are much shorter than yours.
Elapsed time is 0.000002 seconds.
Elapsed time is 0.000002 seconds.
Elapsed time is 0.000001 seconds.
Elapsed time is 0.000000 seconds.
Elapsed time is 0.000000 seconds.
Elapsed time is 0.000000 seconds.
Elapsed time is 0.000000 seconds.
Elapsed time is 0.000000 seconds.
Elapsed time is 0.000000 seconds.
Elapsed time is 0.000000 seconds.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by