Dynamical plotting acceleration during loop calculation

1 visualizzazione (ultimi 30 giorni)
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)?

Risposta accettata

Jan
Jan il 1 Mar 2023
Modificato: Jan il 1 Mar 2023
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 Commenti
Andrei
Andrei il 1 Mar 2023
@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?
Jan
Jan il 1 Mar 2023
@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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by