Setting labels and titles of axes extremely slow

23 次查看(过去 30 天)
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