Why does "tic/toc" output time info before my figure has plotted?

3 views (last 30 days)
I am using a function to plot a somewhat complicated figure with many subplots and lots of data.
I noticed that there was a bit of a lag developing with my function due to the complexity of the figure and I wanted to investigate where the lag is occurring. When I added tic/toc to my function evaluation, I noticed that the output from tic/toc suggests that the script runs in about 0.5 seconds, but there is probably a 1 to 1.5 second lag between the tic/toc output appearing in the Command Window and the figure actually appearing on my screen.
Here is a very simple example which replicates my problem:
tic
figure
for i = 1:6
x = randn(1000000,1);
y = randn(size(x));
subplot(3,2,i);
plot(x,y,'.b');
end
toc
In this example, the Command Window outputs around 0.3 seconds of elapsed time using tic/toc. But there is a noticeable lag of probably about 1 second (or even 2 seconds!) on my computer before the figure actually appears.
Why doesn't MATLAB's tic/toc account for this time? What's happening inside MATLAB (or outside of it) to cause this lag?
When I look at my system performance there is negligible impact on CPU or Memory, with only a small increase in GPU usage. I feel like my system should be able to handle much more complicated graphics without lagging.
Any info is appreciated.
Specs: I am using MATLAB 2020a running on Windows 10 (1909). Intel i7-3930K CPU @ 3.2GHz. 32 GB RAM and NVIDIA GeForce GT 520 GPU.

Answers (1)

Walter Roberson
Walter Roberson on 11 Jun 2020
Graphics runs in a separate thread. The data structures for graphics are in main memory, and when a drawnow() or pause() or a few other operations are encountered, the changes in data graphics data structure are transferred to the second thread and then the computation is returned to without waiting for the graphics. The graphics thread needs to analyze the data structures and translate them into graphics operations supported by the underlying graphics system (such as OpenGL) and then invoke the underlying graphics system to do the rendering, which can involve yet another thread (this one under operating system control), as graphics rendering is potentially done in hardware.
When you make a change to a graphics structure such as creating a Primitive Line Object, then MATLAB does not wait for the hardware to render it.
for i = 1:6
x = randn(1000000,1);
y = randn(size(x));
subplot(3,2,i);
plot(x,y,'.b');
end
Although it would not make any difference for the above example, in any situation where you are updating graphics in a loop, for performance you should try to create the graphics structures only once and then update them instead of re-creating them. For example if you had
for N = 1 : 10
for i = 1:6
x = randn(1000000,1);
y = randn(size(x));
subplot(3,2,i);
plot(x,y,'.b');
end
drawnow();
end
then it would be more efficient to:
for i = 6 : -1 : 1; ax = subplot(3,2,i); ph(i) = plot(ax, nan, nan, '.b'); end %reverse order for preallocation performance
for N = 1 : 10
for i = 1 : 6
x = randn(1000000,1);
y = randn(size(x));
set(ph(i), 'XData', x, 'YData', y);
end
drawnow();
end

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!