Matlab - bsxfun 不再比 repmat 快? [英] Matlab - bsxfun no longer faster than repmat?

查看:36
本文介绍了Matlab - bsxfun 不再比 repmat 快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到在 Matlab 中标准化矩阵的最快方法(零均值,单位方差列).这一切都归结为对矩阵中的所有行应用相同操作的最快方法.我读过的每一篇文章都得出相同的结论:使用 bsxfun 而不是 repmat.Mathworks 撰写的这篇文章就是一个例子:http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/

I'm trying to find the fastest way of standardizing a matrix in Matlab (zero mean, unit variance columns). It all comes down to which is the quickest way of applying the same operation to all rows in a matrix. Every post I've read come to the same conclusion: use bsxfun instead of repmat. This article, written by Mathworks is an example: http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/

但是,在我自己的计算机上尝试时,repmat 总是更快.这是我使用与文章中相同的代码得到的结果:

However, when trying this on my own computer repmat is always quicker. Here are my results using the same code as in the article:

m = 1e5;
n = 100;
A = rand(m,n);

frepmat = @() A - repmat(mean(A),size(A,1),1);
timeit(frepmat)

fbsxfun = @() bsxfun(@minus,A,mean(A));
timeit(fbsxfun)

结果:

ans =

    0.0349


ans =

    0.0391

事实上,在这种情况下,无论输入矩阵有多大,我都不可能让 bsxfun 比 repmat 表现得更好.

In fact, I can never get bsxfun to perform better than repmat in this situation no matter how small or large the input matrix is.

有人能解释一下吗?

推荐答案

您正在阅读的大部分建议,包括 Loren 的博客文章,很可能是指旧版本的 MATLAB,其中 bsxfunrepmat 快很多.在 R2013b 中(请参阅链接中的性能"部分), repmat 被重新实现以在应用于数字、字符和逻辑参数时提供巨大的性能改进.在最近的版本中,它可以与 bsxfun 的速度差不多.

Most of the advice you're reading, including the blog post from Loren, likely refers to old versions of MATLAB, for which bsxfun was quite a bit faster than repmat. In R2013b (see the "Performance" section in the link), repmat was reimplemented to give large performance improvements when applied to numeric, char and logical arguments. In recent versions, it can be about the same speed as bsxfun.

就其价值而言,在我装有 R2014a 的机器上,我得到了

For what it's worth, on my machine with R2014a I get

m = 1e5;
n = 100;
A = rand(m,n);

frepmat = @() A - repmat(mean(A),size(A,1),1);
timeit(frepmat)

fbsxfun = @() bsxfun(@minus,A,mean(A));
timeit(fbsxfun)

ans =
      0.03756
ans =
     0.034831

所以看起来 bsxfun 仍然稍微快一点,但不多 - 在您的机器上,情况似乎正好相反.当然,如果您改变 A 的大小或您正在应用的操作,这些结果可能会再次发生变化.

so it looks like bsxfun is still a tiny bit faster, but not much - and on your machine it seems the reverse is the case. Of course, these results are likely to vary again, if you vary the size of A or the operation you're applying.

可能还有其他原因更喜欢一种解决方案,例如优雅(如果可能,我更喜欢 bsxfun).

There may still be other reasons to prefer one solution over the other, such as elegance (I prefer bsxfun, if possible).

编辑:评论者要求使用 bsxfun 的具体原因,暗示它可能使用比 repmat 更少的内存,以避免临时复制 repmat 没有的内容.

Edit: commenters have asked for a specific reason to prefer bsxfun, implying that it might use less memory than repmat by avoiding a temporary copy that repmat does not.

我认为事实并非如此.例如,打开任务管理器(或 Linux/Mac 上的等效项),观察内存水平,然后输入:

I don't think this is actually the case. For example, open Task Manager (or the equivalent on Linux/Mac), watch the memory levels, and type:

>> m = 1e5; n = 8e3; A = rand(m,n);
>> B = A - repmat(mean(A),size(A,1),1);
>> clear B
>> C = bsxfun(@minus,A,mean(A));
>> clear C

(调整 mn 直到跳转在图中可见,但不要太大以至于内存不足).

(Adjust m and n until the jumps are visible in the graph, but not so big you run out of memory).

我看到 repmatbsxfun 的行为完全相同,即内存平稳上升到新水平(基本上是 A) 没有临时的额外峰值.

I see exactly the same behaviour from both repmat and bsxfun, which is that memory rises smoothly to the new level (basically double the size of A) with no temporary additional peak.

即使操作就地完成也是如此.再次观察内存并输入:

This is also the case even if the operation is done in-place. Again, watch the memory and type:

>> m = 1e5; n = 8e3; A = rand(m,n);
>> A = A - repmat(mean(A),size(A,1),1);
>> clear all
>> m = 1e5; n = 8e3; A = rand(m,n);
>> A = bsxfun(@minus,A,mean(A));

再次,我看到 repmatbsxfun 的行为完全相同,即内存上升到峰值(基本上是 A),然后回退到上一级.

Again, I see exactly the same behaviour from both repmat and bsxfun, which is that memory rises to a peak (basically double the size of A), and then falls back to the previous level.

所以恐怕我看不出repmatbsxfun 在速度或内存方面有太大的技术差异.我对 bsxfun 的偏好实际上只是个人偏好,因为它感觉更优雅一些.

So I'm afraid I can't see much technical difference in terms of either speed or memory between repmat and bsxfun. My preference for bsxfun is really just a personal preference as it feels a bit more elegant.

这篇关于Matlab - bsxfun 不再比 repmat 快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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