- The 'drawnow' command can be used to force MATLAB to flush the output buffer and display the results immediately.
tic toc delayed output inside for loop
12 views (last 30 days)
Show older comments
I want to use the tic toc function to time each iteration of a for loop. Usually, this outputs the time as "Elapsed time is ... seconds." in the command window after each loop iteration. So for the following example code there would be an output approximately every 0.1 seconds on my machine.
for j = 1:20
tic
eig(rand(500));
toc
end
However, since a few days the outputs to the command window are only made after the loop finishes and I get all the 20 outputs for the 20 iterations at once in the end. I don't understand why this changed, I didn't (knowingly) change any settings and couldn't find anything, so maybe it is a bug? I updated my release version hoping that would fix it, but it didn't. I know that this behavior happens when you use tic toc inside a for loop, while a parallel pool is open, but in this case there is no parallel pool. Is there something I can do to get the old behavior where the elapsed time is printed to the command window after each iteration?
5 Comments
Jatin
on 10 Sep 2024
As you pointed out, the toc function should display the duration after each iteration. You might consider reporting this as a bug to MathWorks. However, I'm curious to know if obtaining the durations iteratively adds any value to your objective compared to getting all durations at once.
Answers (2)
Jatin
on 9 Sep 2024
The script you provided does work as intended. According to the documentation for the "tic" and "toc" functions, it is mentioned that "Sometimes programs run too fast for tic and toc to provide useful data. If your code executes in less than 1/10 of a second, consider running it in a loop and then averaging the results to find the time for a single execution."
This implies that if the code executes very quickly, it might appear as though all the output is printed simultaneously after the loop finishes. However, in reality, the output is being queued as intended by the toc function.
The below example code shows that the output to command window are not made only after the loop ends:
%toc prints the elapsed time after each loop
for j = 1:10
tic
pause(1)
toc
end
As noted by @Sam Marshalik, we can observe the output after each iteration. However, what appears to be a bug might actually be due to the "tic" and "toc" functions' inability to measure precision beyond 1/10th of a second, as stated in the documentation.
Here is the documentation for "tic" and "toc" functions for more details:
Hope this helps!
0 Comments
Shivam
on 11 Sep 2024
Edited: Shivam
on 11 Sep 2024
You can store the elapsed time in a variable and use fprintf or disp function to print the elapsed time. It will print the elapsedTime after each iteration.
Here is the workaround you can follow:
for j = 1:20
tic; % Start the timer
eig(rand(500));
elapsedTime = toc; % Stop the timer and get the elapsed time
fprintf('Iteration %d: Elapsed time is %.4f seconds\n', j, elapsedTime);
end
Hope it helps.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements 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!