필터 지우기
필터 지우기

Setting labels and titles of axes extremely slow

조회 수: 13 (최근 30일)
JM
JM 2017년 12월 14일
댓글: Justin 2021년 2월 13일
In my use case the setting of labels and the title of axes takes up to a second or more each and I would like to know why and how I can avoid this.
My use case is as follows: I have programmatically created a 1300x800 figure using:
mainfig = figure(...
'Units','Pixels',...
'Position',[50 70 1300 80],...
'Name','abc',...
'DockControls','off',...
'Resize','off',...
'NumberTitle','off');
and added about 6~8 custom sized axes to it using:
axes(...
'Parent',mainfig,
'Units','pixels',
'Position',ex [1 2 3 4]);
Creating this window, these axes and the plots that use these axes using:
plot(ax,...)
is fine and takes up next to no time in the profiler.
However when I add labels and a title to the axes I am seeing up to 1.5 seconds of runtime for each item set. Here is an example from the MATLAB R2015a profiler (problem occurs in R2015a, R2016b, R2017b):
Line Code Calls Total Time %Time
48 ax.XLabel.String = 'blablabla... 1 1.412 s 33.3%
50 ax.Title.String = 'blablabla... 1 1.379 s 32.5%
49 ax.YLabel.String = 'blablabla... 1 1.375 s 32.4%
...
The profiler shows these as 100% self time and does not show the innards of these lines.
Also, in this case using xlabel() or ax.XLabel.String makes no difference in time cost.
So, to repeat, I would like to know why and how I can avoid this. It should not be normal that putting a single piece of text somewhere in a GUI costs an eternity of CPU time.
A test case is provided here: https://pastebin.com/wBjxydju
  댓글 수: 5
Massimo Ciacci
Massimo Ciacci 2018년 7월 22일
편집: Stephen23 2018년 7월 22일
For me the delay depends on the data size. I guess profiler assigns the time to the lines following plot instead on to the plot itself...
Version: '4.6.0 NVIDIA 391.35'
Vendor: 'NVIDIA Corporation'
Renderer: 'GeForce GTX 1070/PCIe/SSE2'
MaxTextureSize: 32768
Visual: 'Visual 0x09, (RGBA 32 bits (8 8 8 8), Z depth 24 bits, Hardware acceleration, Doub...'
Software: 'false'
SupportsGraphicsSmoothing: 1
SupportsDepthPeelTransparency: 1
SupportsAlignVertexCenters: 1
Extensions: {375x1 cell}
MaxFrameBufferSize: 32768
Massimo Ciacci
Massimo Ciacci 2019년 9월 24일
I confirm that quering properties of axis is slower when there is a lot of stuff plotted on them. A good workaround which I found myself again to use today is to fix all axis properties BEFORE plotting anything on them.

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

답변 (1개)

Massimo Ciacci
Massimo Ciacci 2019년 10월 8일
I think I found a nice workaround
% (1) set all curves invsibile, for a good speed up for label handles retrieval
axChild = get(gca,'Children');
allCurves = findobj(axChild,'type','line');
set(allCurves,'visible','off');
% (2) fix/change labels
% (3) restore visible
set(allCurves,'visible','on');
as an example you can try the following plot code which plots 20M points on two curves
close all
clearvars
tic
profile off
profile on
SPEED_UP = 1;
NN = 20e6;
figure(100); grid on; hold on; axh=gca;
plot(1:NN,1:NN);
plot(1:NN,-(1:NN));
if SPEED_UP
axChild = get(gca,'Children');
allCurves = findobj(axChild,'type','line');
set(allCurves,'visible','off');
end
xlabel('x'); ylabel('y');
set(axh,'xscale','log');
xlh = get(axh,'xlabel');
if SPEED_UP
%Restore visibility
set(allCurves,'visible','on');
end
profile viewer
toc
Elapsed time is 2.147400 seconds. SPEED_UP = 0
Elapsed time is 0.690461 seconds. SPEED_UP = 1
profiler_before_after_NEW.png
  댓글 수: 2
Massimo Ciacci
Massimo Ciacci 2019년 10월 8일
I also noticed that this trick can help speed up linkaxes in the same way, by setting allCurves to invisible in both axes before linking ...
Justin
Justin 2021년 2월 13일
Strangely neither workaround (setting labels before plotting nor hiding graph objects when setting labels) seems to work when you are plotting histograms rather than lines. At least in 2020b anyway.

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

카테고리

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