MATLAB scatter3,plot3速度差异 [英] MATLAB scatter3, plot3 speed discrepencies

查看:499
本文介绍了MATLAB scatter3,plot3速度差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于MATLAB如何花费截然不同的时间绘制同一件事的原因-以及为什么.

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

我在3D空间中生成了10000点:

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:

我关闭了所有图形并清除了每次运行之间的工作空间,以确保公平.

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

使用散点图3进行批量绘图:

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

使用散点图3进行单独绘制:

>> 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.

使用plot3进行批量绘图:

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

使用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.

MATLAB在更长的"例程中花了这么长时间在幕后做什么?使用每种方法的优点和缺点是什么?

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?

修改: 多亏了Ben Voigt的建议(请参阅答案),我在时间中加入了drawnow命令-但这与时间没有多大区别.

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.

推荐答案

运行scatter3plot3所需时间之间的主要区别在于编译plot3而解释scatter3的事实(如您在编辑功能时所看到的).如果同时编译scatter3,则速度差异也会很小.

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.

在循环中绘制与在一次绘制中所需的时间之间的主要区别是,您将绘制的手柄作为子代添加到坐标轴上(请查看get(gca,'Children')的输出),然后因此,我们在循环内增长了一个复杂的数组,我们都知道这很慢.此外,您经常调用多个函数,而不仅仅是一次调用,因此会导致函数开销.

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

这会强制Matlab在每次迭代时更新图形(并且速度要慢很多),您会看到轴的限制根本没有改变(因为默认的轴限制为0和1).但是,即使轴的限制以不同的方式开始,它们也不需要花费很多迭代就可以收敛到这些数据.与省去hold on相比,省去了hold on的时间,这是因为轴的每一步都会重新计算.

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.

为什么有这些不同的功能? scatter3允许您在单个手柄下绘制具有不同标记大小和颜色的点,而您需要一个循环并使用plot3为每个点获取一个手柄,这不仅在速度上很昂贵,而且在记忆方面.但是,如果您需要分别与不同的点(或点组)进行交互-也许您想为每个点添加一个单独的图例条目,也许您希望能够分别打开和关闭它们,等等-使用plot3循环可能是最好的(尽管很慢)解决方案.

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.

这篇关于MATLAB scatter3,plot3速度差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆