Dynamical plotting acceleration during loop calculation

조회 수: 1 (최근 30일)
Andrei
Andrei 2023년 2월 28일
댓글: Jan 2023년 3월 1일
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
Mathieu NOE
Mathieu NOE 2023년 3월 1일
hello
do you have a (simpliied) code to share ?
Andrei
Andrei 2023년 3월 1일
Hello, added, thank for answering

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2023년 3월 1일
편집: Jan 2023년 3월 1일
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
Andrei
Andrei 2023년 3월 1일
@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 2023년 3월 1일
@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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by