## Significant differences in execution time

### Stefan Kopecz (view profile)

on 24 Jun 2014
Latest activity Commented on by Titus Edelhofer

on 8 Jul 2014

### Titus Edelhofer (view profile)

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?

Sara

### Sara (view profile)

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 (view profile)

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...

### Products ### Titus Edelhofer (view profile)

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

Stefan Kopecz

### Stefan Kopecz (view profile)

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

on 8 Jul 2014
Hi Stefan,