Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why matlab execution time differs when pressing F9

Tags:

time

matlab

I have a pretty simple situation where matlab execution time differs depending on the way I run the code :

tic
pause(1)
toc

If I press Ctrl+Enter on a script, I get something like :

Elapsed time is 1.000241 seconds.

If I select the code and press F9, I get something like :

Elapsed time is 1.025203 seconds

I notice a 25ms time difference which is reproducible.

like image 885
Hugo Trentesaux Avatar asked Jan 17 '26 10:01

Hugo Trentesaux


1 Answers

if you copy and past your code in Matlab "Command Window" you see big difference as compare running your code as a m file.

on my computer (Intel i7, 16GB RAM), my result is varying +/- 5ms.

Elapsed time is 1.002843 seconds.
Elapsed time is 1.008236 seconds.
Elapsed time is 1.003001 seconds.

but as I run the code in a m file, I get much accurate result. +/- 0.01ms

Elapsed time is 1.000032 seconds.
Elapsed time is 1.000041 seconds.
Elapsed time is 1.000021 seconds.

and the possible reason is: If you run the code as m file, matlab try to pars the code in advance and your code will be run quicker and there is no intermediate parsing phase for each individual lines.

like image 160
A. Fasih Avatar answered Jan 20 '26 01:01

A. Fasih



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!