Commands faster in one line

9 views (last 30 days)
Patrick
Patrick on 2 Jul 2014
Commented: dpb on 2 Jul 2014
When I run basic arithmetic, and simple memory allocation tasks in the command window (say matrix multiplication and variable assignment, why is it so much faster to run all the statements in a single line separated by semicolons rather than multiple line with a single tsatement on each? Example:
tic;A = rand(5);B = rand(5);C = A*B;toc
Elapsed time is 0.000039 seconds.
>> tic;
A = rand(5);
B = rand(5);
C = A*B;
toc
Elapsed time is 0.014997 seconds.

Accepted Answer

dpb
dpb on 2 Jul 2014
Because at the command line each line is parsed and the JIT compiler/optimizer is pretty much disabled. In an m-file, the file is parsed and then executed.
  2 Comments
Patrick
Patrick on 2 Jul 2014
Ah, so parsing is the rate limiting step here? whereas in an m file, the whole thing is only parsed once, so it doesn't suffer from this same effect?

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!