MATLAB: Tic, Toc Behavior

MATLABspeed

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

Best Answer

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