Tic, Toc Behavior

19 ビュー (過去 30 日間)
Cillian Hayde
Cillian Hayde 2020 年 11 月 19 日
コメント済み: Cillian Hayde 2020 年 11 月 19 日
Hi,
I'm curious about the behaviour of Tic-Toc.
Tic-Toc excutes faster in a loop than outside a loop.
Does anyone know exactly why? Some sort of JIT compiliation issue?
Loop Version
TicTocLoop = 0;
for i =1:1:1
tic;
TicTocLoop(i) = toc;
end
TicTocLoop
TicTocLoop = 4.8400e-05
Non-Loop Version
TicTocValue = 0;
tic;
TicTocValue = toc;
TicTocValue
TicTocValue = 0.0011
My setup is
MATLAB 2020b
Windows 10
Using Live Script
Thanks for any help
Regards
Cillian

採用された回答

Fangjun Jiang
Fangjun Jiang 2020 年 11 月 19 日
for the sake of fair comparison, the code below should give you reasonable results
%%
TicTocLoop=0;
TicTocValue=0;
for i =1:1:1
tic;
TicTocLoop = toc
end
tic;
TicTocValue=toc
TicTocLoop =
2.7100e-05
TicTocValue =
1.2300e-05
TicTocLoop =
2.4100e-05
TicTocValue =
1.3000e-05
TicTocLoop =
2.4400e-05
TicTocValue =
1.3300e-05
  1 件のコメント
Cillian Hayde
Cillian Hayde 2020 年 11 月 19 日
Hey Fangjun Jiang,
It appears your answer has helped me. It appears to be a live editor issue.
To my eye, tic-toc in a loop is the same for both the m-file and the mlx-file.
The non-looped version is the fastest in the m-file and slowest in the mlx-file.
I personally have no notion why this is, but I will just note it!
Thanks for that!
Regards
Cillian

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

その他の回答 (1 件)

John D'Errico
John D'Errico 2020 年 11 月 19 日
Using tic and toc to masure any time is a bad idea.
Using tic and toc to masure any time is a bad idea.
Using tic and toc to masure any time is a bad idea.
There, I've said it three times, so it must be true.
Use better tools, best, use timeit. Also you can use the profiler tool, although profiled code will run more slowly, since MATLAB needs to maintian statistics on everything.
  2 件のコメント
Rik
Rik 2020 年 11 月 19 日
編集済み: Rik 2020 年 11 月 19 日
Not bad, just often misused. tic and toc should be reserved for situations where you don't care about precision beyond (let's grab a random number) 0.1 second. Actual profiling should be done with the profiler, timing should be done with timeit, and counting seconds can be done with tic. toc.
It is almost as if these names were not chosen randomly (even if tictoc predates the other two).
John D'Errico
John D'Errico 2020 年 11 月 19 日
True about the misused statement. :)
And at times, I use tic and toc myself. But I only ever do that if I warm up the functions in advance, and only for cases when the multiple runs that timeit will perform would be way to time intensive.

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

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by