Hugo Trentesaux
Hugo Trentesaux

Reputation: 1999

Why matlab execution time differs when pressing F9

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.

Upvotes: 3

Views: 128

Answers (2)

matlabgui
matlabgui

Reputation: 5672

I suspect this is due to the fact that the sections of code which are run when you select Ctrl+Enter are already stored in memory so they are "ready to run", whereas when you press F9 Matlab has to read whats selected from the editor after you press F9.

Upvotes: 2

A. Fasih
A. Fasih

Reputation: 93

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.

Upvotes: 3

Related Questions