Need help computing average time for a code I made

1 vue (au cours des 30 derniers jours)
Peter Phung
Peter Phung le 6 Nov 2017
My professor wants me to generate a loop that will run my code 10 times. For those 10 times he wants me to compute the time for each loop and find the average time. So I am supposed to get ten values for the time(t) and divide that value by 10 to get the average time it takes to run the program. My classmates and me have come to the conclusion that the loop (see code on bottom) we made should give us the average time but we are not sure. The last line should print out the final answer for the loop.
for i = 1:10
tic
someCode
t(i) = toc
end
disp(sum(t)/10)

Réponses (1)

Star Strider
Star Strider le 6 Nov 2017
That should work. You can also use the mean function:
for i = 1:10
tic
someCode
t(i) = toc;
end
disp(sum(t)/10)
disp(mean(t))

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by