Dynamical plotting acceleration during loop calculation

Currently I am solving physical problem, where I need to see what is happening during the solution. Because of that I dynamically draw plots of absolute value and angle. But it increases work time of the problem (from 0.005 s to 0.2 s per loop). Is there a way to plot at the same speed as the calculation is performed (or maybe there is another type of plotters)?

2 Comments

hello
do you have a (simpliied) code to share ?
Hello, added, thank for answering

Sign in to comment.

 Accepted Answer

Without seeing the code, it is hard to guess the reason of the slow down. I dare to guess boldly, that you insert new objects in an existing axes or even worse create new axes without deleting the older ones. This is extremely expensive. Examples:
% Wastes minutes to hours:
tic;
FigH = figure;
data = [];
for k = 1:1000
data(k, :) = rand(1, 2);
axes; % Bad idea!
plot(data(:, 1), data(:, 2), '*');
drawnow;
end
toc
% Keep axes, replace line object:
tic;
FigH = figure;
axesH = axes('NextPlot', 'replace'); % Re-use axes object
data = [];
for k = 1:1000
data(k, :) = rand(1, 2);
plot(axesH, data(:, 1), data(:, 2), '*'); % All points redrawn
drawnow;
end
toc
% Add new points only:
tic
FigH = figure;
axesH = axes('NextPlot', 'add');
data = [];
for k = 1:1000
data(k, :) = rand(1, 2);
plot(axesH, data(k, 1), data(k, 2), 'b*'); % Add new points only
drawnow;
end
toc
% Re-use line object:
tic;
FigH = figure;
axesH = axes('NextPlot', 'add');
lineH = plot(1, 1, '*');
data = [];
for k = 1:1000
data(k, :) = rand(1, 2);
set(lineH, 'XData', data(:, 1), 'YData', data(:, 2)); % Update line properties
drawnow;
end
toc
There are several options to avoid the exhausting of graphic resources.

7 Comments

Hello, I provided code. So opening and closing figure every loop is a poor choice of realization of my task? I aimed to save up some memory, since I havent got much RAM and task is kinda resourceful, so I decided to close figure so that it doesn't stay in memory. Also there is problem with sizing of cbars, but its another topic. Thanks for your advices!
@Andrei: Creating new figures is expensive also. It is cheaper for the CPU and the RAM to re-use existing figures, axes and line objects. Staying in memory is more efficient to create a pile of figures and deleting them.
The clear command is not useful for similar reasons.
The exp() function is expensive. Avoid to calculate it twice for the same argument, e.g. 1i*(S.*exp(-1i*eps1*(nn+1*0.5)*dt)).
Try to create the figure once only and update the contents of the axes and the title.
Another option that's similar to the "Add new points only" example @Jan posted is to use an animatedline. You can create it before the loop and addpoints to it inside the loop.
@Steven Lord I am plotting surface, calculated numerically, I think your solution is useful, but for 2D plots with known shape. Thanks anyway!
@Jan Thanks, got it. Will fix exponents also. u gave me good idea
@Jan By the way, could you recommend me a book where I can learn which operations are more RAM-expensive so that I could optimize my codes in the future?
@Andrei: I do not know a certain book, which clarifies this topic exhaustively. Most of all it depends on the Matlab version.
E.g. one call of eval() or load() without catching the output in a variable anywhere in the code can disable Matlab's JIT acceleration and change the memory consumption systematically. The JIT is not documented on purpose, so it is not easy to write general instructions.
Search in the net, especially in the FileExchange for manuals about efficient code. But even then, think twice: It was an important paradigma to vectorize code in Matlab, but the speed of CPUs has grown faster than the speed of the memory access. If some temporary data do not fit into the CPU cache, loops can be much faster then vectorized code. MathWorks has improved the processing of loops in the last 20 years massively. Although I know this, it is hard for me to suggest ugly C-style loops in Matlab here in the forum.
I've learned programming on a 1kB ZX81 in the 1980th. To squeeze an ODE integrator in this tiny memory I even stored all constants as strings, because this had a smaller memory foot print. Although I do have experiences with really memory efficient programming, I consider this as ancient art. Instead of writing ugly code to save RAM, I suggest to install more RAM. While smart coding can speedup code clearly and often more than an expensive CPU upgrade, nothing can beat real RAM except for more real RAM.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Asked:

on 28 Feb 2023

Commented:

Jan
on 1 Mar 2023

Community Treasure Hunt

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

Start Hunting!