Why does it take so long to delete hggroup objects?

1 view (last 30 days)
I'm using the impoly function from the Image Processing Toolbox to draw regions on a somewhat large image. When these regions have many vertices, 500+, it takes a long time (20+ seconds) to delete the impoly handle.
The hggroup object seems to essentially be a structure that contains an array of vertices/vectors. Why does it take so long to delete if that array has over a few hundred objects?
  2 Comments
Walter Roberson
Walter Roberson on 24 Jul 2015
Which MATLAB release are you using? In particular is it R2014b or later, or is it an earlier version?
AFiorillo
AFiorillo on 27 Jul 2015
I'm using R2015a (8.5.0.197613) with the Image Processing Toolbox v9.2.

Sign in to comment.

Accepted Answer

Mike Garrity
Mike Garrity on 28 Jul 2015
It's not actually the hggroup objects. Deleting those is actually quite quick. It's all of the other stuff that's happening when you delete the hggroup objects. The profiler doesn't have a good way to tell you about all of the details.
Consider this example:
n = 5000;
%%Just Groups
clf
drawnow
h = gobjects(1,n);
tic
for i=1:n
h(i) = hggroup;
end
drawnow;
disp(['Just the groups, create = ' num2str(1e3*toc/n) ' milliseconds'])
tic
delete(h)
drawnow
disp(['Just the groups, delete = ' num2str(1e3*toc/n) ' milliseconds'])
%%Groups with Children
clf
drawnow
h = gobjects(1,n);
tic
for i=1:n
h(i) = hggroup;
line([i i],[-1 1],'Parent',h(i));
end
drawnow
disp(['Groups with lines, create = ' num2str(1e3*toc/n) ' milliseconds'])
tic
delete(h)
drawnow
disp(['Groups with lines, delete = ' num2str(1e3*toc/n) ' milliseconds'])
%%Groups with Children and ButtonDownFcns
clf
drawnow
h = gobjects(1,n);
tic
fcn = @(~,~)disp(i);
for i=1:n
h(i) = hggroup;
line([i i],[-1 1],'Parent',h(i));
set(h(i),'ButtonDownFcn',fcn)
end
drawnow
disp(['Groups with ButtonDownFcn, create = ' num2str(1e3*toc/n) ' milliseconds'])
tic
delete(h)
drawnow
disp(['Groups with ButtonDownFcn, delete = ' num2str(1e3*toc/n) ' milliseconds'])
If you run this, you'll see that the delete gets progressively slower in each of the three cases.
In the second case, we've added children to the hggroups. These get deleted when the hggroups get deleted. That costs something. The hggroup objects that impoly returns have couple of children to draw the polygon.
In the third case, we've added function handles to the hggroups. The hggroups which are created by impoly have a bunch of function handle properties added to them to implement all of the interactive behavior. These function handles have to be cleaned up when the hggroup objects are deleted.
  1 Comment
AFiorillo
AFiorillo on 28 Jul 2015
Thank you for the detailed answer! This helped clarify the nature of a huge slow-down in my project.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!