Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB scatter3, plot3 speed discrepencies

This is about how MATLAB can take very different times to plot the same thing — and why.

I generate 10000 points in 3D space:

X = rand(10000, 1);
Y = rand(10000, 1);
Z = rand(10000, 1);

I then used one of four different methods to plot this, to create a plot like so:

alt text

I closed all figures and cleared the workspace between each run to try to ensure fairness.

Bulk plotting using scatter3:

>> tic; scatter3(X, Y, Z); drawnow; toc
Elapsed time is 0.815450 seconds.

Individual plotting using scatter3:

>> tic; hold on;
for i = 1:10000
    scatter3(X(i), Y(i), Z(i), 'b');
end
hold off; drawnow; toc
Elapsed time is 51.469547 seconds.

Bulk plotting using plot3:

>> tic; plot3(X, Y, Z, 'o'); drawnow; toc
Elapsed time is 0.153480 seconds.

Individual plotting using plot3:

>> tic; hold on
for i = 1:10000
    plot3(X(i), Y(i), Z(i), 'o');
end
drawnow; toc
Elapsed time is 5.854662 seconds.

What is it that MATLAB does behind the scenes in the 'longer' routines to take so long? What are the advantages and disadvantages of using each method?

Edit: Thanks to advice from Ben Voigt (see answers), I have included drawnow commands in the timing — but this has made little difference to the times.

like image 398
Bill Cheatham Avatar asked Nov 06 '10 00:11

Bill Cheatham


2 Answers

The main difference between the time required to run scatter3 and plot3 comes from the fact that plot3 is compiled, while scatter3 is interpreted (as you'll see when you edit the functions). If scatter3 was compiled as well, the speed difference would be small.

The main difference between the time required to plot in a loop versus plotting in one go is that you add the handle to the plot as a child to the axes (have a look at the output of get(gca,'Children')), and you're thus growing a complicated array inside a loop, which we all know to be slow. Furthermore, you're calling several functions often instead of just once and incur thus calls from the function overhead.

Recalculation of axes limits aren't an issue here. If you run

for i = 1:10000
    plot3(X(i), Y(i), Z(i), 'o');
    drawnow;
end

which forces Matlab to update the figure at every iteration (and which is A LOT slower), you'll see that the axes limits don't change at all (since the default axes limits are 0 and 1). However, even if the axes limits started out differently, it wouldn't take many iterations for them to converge with these data. Compare with omitting the hold on, which makes plotting take longer, because axes are recalculated at every step.

Why have these different functions? scatter3 allows you to plot points with different marker sizes, and colors under a single handle, while you'd need a loop and get a handle for each point using plot3, which is not only costly in terms of speed, but also in terms of memory. However, if you need to interact with different points (or groups of points) individually - maybe you want to add a separate legend entry for each, maybe you want to be able to turn them on and off separately etc - using plot3 in a loop may be the best (though slow) solution.

like image 190
Jonas Avatar answered Nov 16 '22 05:11

Jonas


For a faster approach, consider this third option (directly uses the low-level function LINE):

line([X,X], [Y,Y], [Z,Z], 'LineStyle','none', 'Marker','o', 'Color','b')
view(3)

Here are some articles discussing plotting performance issues:

  • Performance: scatter vs. line
  • Plot performance
like image 6
Amro Avatar answered Nov 16 '22 05:11

Amro