Reputation: 1660
I am dealing with a structured grid. I display it with patch and I just tried to add to the plot a text of the type (m,n) that indicates the indices of each node. I use the text function, and in the future I'd like to plot the nodal values of the variables instead (that are time varying, that's why the text function is inside the loop). I profiled the code and most of the time is spent in the drawnow, cla, and text functions:
drawnow 7.882 s 51.7%
text(x(:),y(:),charINPUT(:),'C... 4.348 s 28.5%
cla 2.300 s 15.1%
I opened a thread here "Text" function very slow, bottleneck of my code to understand if its possible to improve the performance of the text function or if any faster alternative is available. Here I'd like to figure out why drawnow and cla are so slow. If I remove cla it gets even slower:
drawnow 42.774 s 86.2%
text(x(:),y(:),charINPUT(:),'C... 4.638 s 9.3%
with drawnow taking a lot. Adding cla helped but still so slow!
It is only a 71 x 71 grid, if you increase the number of cells the code is basically stuck. Any suggestion to speed this up? (Note: I use patch because I want to plot some grid quantities for each cell and I want to keep it general in case I move to an unstructured mesh with irregular polygons that moves in times, that's why it's inside the time stepping.
EDIT: I first thought patch was pretty-fast, cause it was taking only few milliseconds, but as noted by grantnz in his comment below, its actually drawn when drawnow is called. If the line hpatch3 = ... is commented in my code provided below, this is the timing with the profiler:
text(x(:),y(:),charINPUT(:),'C.. 4.539 s 58.4%
cla 2.285 s 29.4%
drawnow 0.576 s 7.4%
Therefore now things go a bit bitter but still pretty slow. It looks like creating and drawing the patch takes about 7 secs, and about 8 secs for creating and drawing the text. The main difference is that with text the time is mainly spent in the function itself (and in cla) while for patch it is spent all with drawnow. Any idea why this happens? Is it possible to speed this up a bit? Here is my code.
%define grid and grid numbering
ntimesteps = 10
DX = 10 ; %=DY
mmax = 71; %= number of nodes in x
nmax = mmax %= number of nodes in y
[ x y ] = meshgrid(0:DX:DX*(mmax-1),0:DX:DX*(mmax-1)); %grid
[ mMAT nMAT ] = meshgrid(1:mmax,1:nmax); %grid numbering
for j=1:ntimesteps
cla
%
%display patch
%
cont = 0
for m=2:mmax
for n=2:nmax
cont=cont+1;
Xpatch(1:4,cont) = [ x(n-1,m-1) ; x(n-1,m) ; x(n,m) ; x(n,m-1) ] ;% ii+1 since it has the BC
Ypatch(1:4,cont) = [ y(n-1,m-1) ; y(n-1,m) ; y(n,m) ; y(n,m-1) ] ;
Zpatch(cont) = 1;
end
end
hpatch3 = patch(Xpatch(:,:),Ypatch(:,:),Zpatch(:)');
%
% display node indices
%
charINPUT = regexp(sprintf('(%d,%d)\n',mMAT(:),nMAT(:)),'(?<=\s*)(\S*)(?=\n)','match'); % use regexp to vectorize sprintf and so avoid slow loops with sprintf
text(x(:),y(:),charINPUT(:),'Clipping', 'on');
set(gcf,'position',[9 40 1350 650])
set(gcf,'PaperPositionMode','auto')
%
% other things added to the plot
%
drawnow
end
Upvotes: 0
Views: 1791
Reputation: 879
For your example, I can get a speed up of a x2 by changing the renderer to painters
set(gcf, 'renderer', 'painters')
According to the documentation
painters — The original rendering method used by MATLAB is faster when the figure contains only simple or small graphics objects.
Depending on what your patches are in real life, you may need to stick with either the zbuffer or openGL renderers. You may be able to get better performance by using low level OpenGL calls to place the text objects, but this would be a lot of work.
Upvotes: 1