Significant differences in execution time
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Stefan Kopecz
il 24 Giu 2014
Commentato: Titus Edelhofer
il 8 Lug 2014
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 Commenti
Sara
il 24 Giu 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
il 24 Giu 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...
Risposta accettata
Titus Edelhofer
il 24 Giu 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 Commenti
Titus Edelhofer
il 8 Lug 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
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!