Need help computing average time for a code I made

조회 수: 1 (최근 30일)
Peter Phung
Peter Phung 2017년 11월 6일
답변: Star Strider 2017년 11월 6일
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