MATLAB Answers

Significant differences in execution time

37 views (last 30 days)
I'd like to measure the execution time of two different operations. I see significant differences when executing a script or executing the same commands in the command window.
Command Window:
>> clear all
>> n = 1e7; x = randn(n,1);
>> tic, s = 0; for i=1:n, s = s + x(i)^2; end, toc
Elapsed time is 0.396205 seconds.
>> tic, s = sum(x.^2); toc
Elapsed time is 0.031208 seconds.
Script (timings.m):
clear all
n = 1e7; x = randn(n,1);
tic, s = 0; for i=1:n, s = s + x(i)^2; end, toc
tic, s = sum(x.^2); toc
Calling the script results in:
>> timings
Elapsed time is 7.230688 seconds.
Elapsed time is 0.085515 seconds.
So the for loop is about 20 times slower compared to the command window execution!
Script (timings2.m):
clear all
n = 1e7; x = randn(n,1);
tic, s = 0; for i=1:n, s = s + x(i)^2; end, toc
clear all
n = 1e7; x = randn(n,1);
tic, s = sum(x.^2); toc
Calling the script results in:
>> timings2
Elapsed time is 0.036559 seconds.
Elapsed time is 0.036499 seconds.
Now the for loop is about 10 times faster compared to the command window execution!
What is the reason for these differences in execution time? How should one measure the execution time of the the different statements?

  2 Comments

Sara
Sara on 24 Jun 2014
When you execute a script more than once, matlab does some optimizations, so that the second time it's faster. You may try to use the profiler to see what is the difference.
Geoff Hayes
Geoff Hayes on 24 Jun 2014
The above behaviour does seem peculiar. Running the following as a script has better performance ( for me ) over timings.m
clear all
n = 1e7; x = randn(n,1);
tic, s = 0; for i=1:n, s = s + x(i)^2; end, toc
tic, v = sum(x.^2); toc
The only difference between the above and the other is the v = sum(x.^2); - so rather than re-using s, I've created a new local variable v. Running it repeatedly (as a script) results in the same execution times of
Elapsed time is 0.241846 seconds.
Elapsed time is 0.049889 seconds.
These results are, strangely enough, the same as if when I run timings2.m. (Which may not be that strange since I'm using a new local variable whereas timings2 is doing the clear all.) In order for me to improve upon these times and obtain similar to what Stefan has shown, I had to remove the commas from the code in the timings2.m script...

Sign in to comment.

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 24 Jun 2014
Hi,
in addition to Sara's comment take a look at the function timeit, that was introduced in R2013b (if you are using an older version, take a look at this file on the fileexchange.
Titus

  2 Comments

Stefan Kopecz
Stefan Kopecz on 7 Jul 2014
Thanks for pointing me to the timeit function. I'm using R2011b and was not aware of this function.
However, this doesn't answer why the exececution of the for loops slows down, when resusing the summation variable s to store the result of the sum function.
Titus Edelhofer
Titus Edelhofer on 8 Jul 2014
Hi Stefan,
some additional comments:
  • Don't write the for loop into one line with commas. Write each statement into one line. This way the interpreter can speed up the execution.
  • Scripts are typically less efficient than functions. Convert your script to a function if you need better performance. For me this cut's execution time roughly in half already.
Titus

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products