Confusion with tic-toc and run times
128 views (last 30 days)
Show older comments
I am trying to figure out how long a simulation will take. I desire to run multiple simulations and average out the result as is descrbide in the code below
tic
repeats = 10; %number of times we repeat the simulation
n = 800; %number of particles
diam = 1/sqrt(n); %diameter of a particle
k = 5; %grid denisty
uppertime = 0.2; %simulation time
timesteps = 40; %number of frames in simulation
MultipleP = zeros(repeats,timesteps+1);
times = zeros(1,repeats+1);
times(1) = toc;
for i = 1:repeats
tic
MultipleP(i,:) = shorttimeoptimised(n,uppertime,timesteps,k);
times(i+1) = toc;
end
time = sum(times);
AverageP = mean(MultipleP);
When I run the above code I get a time value of about 1.2 seconds where if I time the above code with the spot watch on my phone I get about 47 seconds which including my human error at the very lowest the actual run time is 40 seconds. Why is there such a huge dissparity between matlabs run time and my stopwatch timed run time? Additionally, how would I compute the run time for shorttimeoptimised. I have tried giving the function a second output variable which i call time and then introduce a tic and toc at the begining and end of the function. However, when doing this tic toc produces a run time of 0.1 second which is noticably not true.
I appreciate that the variable time doesn't caputre the time taken to go from the end of each for loop to the beginning of next. But I would have thought that this time is negligible for a for loop of length 10.
Furthemore, I am intrested in how the run time for say repeats = 3000. I am not sure if there is some sort of initialising going on at the begining that causes and initial time delay or whether therun time really would be about 40*300 seconds?
Many Thanks,
Milos
2 Comments
the cyclist
on 7 Jun 2023
Because we don't your shorttimeoptimised function, I replaced it with a calculation of the eigenvalues of a large random matrix, such that the "wall clock" time of your code took about 1 minute (actually 59.92 seconds).
Your time variable ended up with 59.47 seconds, very close to wall time, as expected.
Is your shorttimeoptimised function calling tic again?
Accepted Answer
Jim Riggs
on 7 Jun 2023
You might consider commenting out the "tic" command inside the for loop. This will make all of the "times" values relative to the start of the script (the initial "tic" command) and also eliminate 10 "tic" commands. Note that if you are getting tic/toc measurements in the 0.1 second range, these are not reliable. (see toc command, and also Measuring the performance of your code)
More Answers (1)
Steven Lord
on 8 Jun 2023
What does your shorttimeoptimised function return? Does it return how long the operation took to run or does it return the results of those calculations? If the latter, comparing the time variable and the AverageP variable is like comparing apples and oranges.
You may also want to run your shorttimeoptimised function using timeit and let timeit determine how many times it needs to run the code to get an accurate estimate of its average execution time.
2 Comments
Steven Lord
on 14 Jun 2023
From the timeit documentation: "In order to perform a robust measurement, timeit calls the specified function multiple times and returns the median of the measurements. If the function runs fast, timeit might call the function many times."
10/0.21
Without seeing what shorttimeoptimised is doing it's not clear to me that there is a problem. timeit calling the function 50 or so times to obtain a robust measurement doesn't seem unreasonable to me.
See Also
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!