Need help computing average time for a code I made

1 次查看(过去 30 天)
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)

回答(1 个)

Star Strider
Star Strider 2017-11-6
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))

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by