drawnow slower in 2014b ?

3 views (last 30 days)
Andreas
Andreas on 29 Oct 2014
Commented: Mike Garrity on 15 Jan 2015
Hi,
I want to visualize data from a swarming simulation. For this sake, I generate textured surface objects, rotate and place them according to the data from the simulation. I also do an animation, where I manipulate the XData and YData properties of the surface objects and update the figure with drawnow. This all works fine. The problem is the following: The same code runs much faster with the older 2014a version of Matlab. Profiling the code, I realized that much more time is spent for drawnow in 2014b. The speed is relevant to me, since I want to do a real-time application.
The following example should be faster on 2014a than it is on 2014b:
%%Parameters
% texture size in pixels, height and width
px=800;
% angle pciture is rotated in every step
angle=0.2;
% number of updates, rotation steps
fr=100;
%%read a picture
% Choose any png file with transparency
[bc,~,ba]=imread('picture.png');
% Adjust the resolution
bc=imresize(bc,[px,px]);
ba=imresize(ba,[px,px]);
%%Plot surface object
% Object will occupy the region [-1,1]^2
Xr=[-1,1;-1,1];
Yr=[1,1;-1,-1];
% Generate surface object with texure and transparency data
figure(1)
obj=surface(Xr,Yr,zeros(2),...
'FaceColor','texturemap','Cdata',bc,...
'FaceAlpha','texturemap','AlphaData',ba,...
'EdgeColor','none');
axis([-1.5,1.5,-1.5,1.5]);
%%Animation
% Rotation matrix
rotMat=[cos(angle),-sin(angle);sin(angle),cos(angle)]';
% Most of the computing time is spent with calls to drawnow, as can be seen
% in the profile ( "run and time" )
tic;
for k=1:fr
% Get old position data
xx=get(obj,'XData');
yy=get(obj,'YData');
% Rotate them
P=[xx(:),yy(:)]*rotMat;
% set new position data
set(obj,'XData',reshape(P(:,1),[2 2]));
set(obj,'YData',reshape(P(:,2),[2 2]));
% only works in 2014b
%obj.XData=reshape(P(:,1),[2 2]);
%obj.YData=reshape(P(:,2),[2 2]);
% Update figure
drawnow;
end
time=toc
I have done some tests and it appears, that the computing time depends on the texture resolution. When I simulate this on my machine (iMac with Mac OS 10.7 Lion, Core 2 Duo 3 GHz) with different texture resolutions, I get the following computing times for the different Matlab versions:
I have also tried this on a Windows machine (Intel Core i7 860, 2.8 GHz) and observed the same behaviour. If you have read this far, thanks :-) I'm looking forward to your suggestions.
  3 Comments
Trey
Trey on 14 Jan 2015
Update: I tried
set(gcf, 'Renderer', 'painters')
right before drawnow inside inputsdlg (matlab central). 2014b is now just as fast as 2014a. This turns off OpenGL rendering of course, so it may not be of any help in other cases.
Note, there's a lot of discussion at
Trey
Mike Garrity
Mike Garrity on 15 Jan 2015
Painters wouldn't be a good choice here because they're using AlphaData. Before R2014b, painters didn't support AlphaData.
Trey, when the profiler points at drawnow, it's just telling you that the bottleneck is synching up multiple threads, it doesn't tell you anything about why. It's very unlikely that the inputsdlg issue is the same as this one. I don't know anything about inputsdlg, but it doesn't look like it's doing anything with graphics, so moving things into the graphics card's memory is unlikely to be a factor. Have you contacted MathWorks Support?

Sign in to comment.

Accepted Answer

Mike Garrity
Mike Garrity on 15 Jan 2015
What's going on here is that in R2014b, the geometry is stored in the GPU's memory. Because you're modifying the XData & YData on each frame, it has to get pushed across from MATLAB's arrays in the CPU's memory to the GPU's memory. This is can be a slow operation.
This is very similar to what I talk about here:
And the same solution works here:
%%Plot surface object
% Object will occupy the region [-1,1]^2
Xr=[-1,1;-1,1];
Yr=[1,1;-1,-1];
% Generate surface object with texure and transparency data
figure(1)
xfm = hgtransform;
obj=surface(Xr,Yr,zeros(2),...
'FaceColor','texturemap','Cdata',bc,...
'FaceAlpha','texturemap','AlphaData',ba,...
'EdgeColor','none', ...
'Parent',xfm);
axis([-1.5,1.5,-1.5,1.5]);
%%Animation
% Rotation matrix
rotMat=[cos(angle),-sin(angle);sin(angle),cos(angle)]';
% Most of the computing time is spent with calls to drawnow, as can be seen
% in the profile ( "run and time" )
tic;
for k=1:fr
% Rotate them
xfmmat = eye(4);
xfmmat(1:2,1:2) = rotMat;
xfm.Matrix = xfm.Matrix * xfmmat;
% Update figure
drawnow;
end
time=toc
This approach yields a small gain in R2014a, but a very large gain in R2014b.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!