比较BSXFUN和REPMAT [英] Comparing BSXFUN and REPMAT
问题描述
在 bsxfun
和 repmat
之间的性能比较之前,很少有人问过问题.
Few questions were asked before on comparisons between bsxfun
and repmat
for performance.
- 其中之一是:
Matlab - bsxfun no longer faster than repmat?
.这篇文章试图研究repmat
和bsxfun
之间的性能比较,特定于沿着输入数组本身的列对输入数组的均值进行减法运算,因此将仅探索bsxfun
的@minus
部分与其repmat
等价物. - 另一个是:
In Matlab, when is it optimal to use bsxfun?
.那个试图用均值沿列执行相同的减法运算,也没有扩展到其他内置运算.
- One of them was:
Matlab - bsxfun no longer faster than repmat?
. This one tried to investigate performance comparisons betweenrepmat
andbsxfun
, specific to performing subtraction of an input array's mean along the columns from the input array itself and as such would explore only the@minus
part ofbsxfun
against itsrepmat
equivalent. - Another was :
In Matlab, when is it optimal to use bsxfun?
. That one tried to do the same operation of subtraction by the mean along columns and didn't expand onto other built-in operations either.
在这篇文章中,我试图调查 bsxfun
和 repmat
之间的性能数据,以涵盖所有 bsxfun
内建函数可以使它具有更广阔的视野,因为这两个函数均提供了良好的矢量化解决方案.
With this post, I am trying to investigate the performance numbers between bsxfun
and repmat
to cover all the bsxfun
built-ins to sort of give it a wider perspective as both of these present good vectorized solutions.
具体来说,我对这篇文章的疑问是:
Specifically, my questions with this post are:
-
使用
bsxfun
进行的各种内置操作如何针对repmat
等效项执行?bsxfun
支持浮点运算,例如@plus
,@minus
,@times
等,以及关系和逻辑运算,例如@ge
,@and
等.特定的内置功能使我比使用repmat
等同于bsxfun
给我明显的提速?
How do the various built-in operations with
bsxfun
perform againstrepmat
equivalents?bsxfun
supports floating point operations like@plus
,@minus
,@times
, etc. and also relational and logical operations like@ge
,@and
, etc. So, are there specific built-ins that would give me noticeable speedups withbsxfun
than using theirrepmat
equivalents?
她的> strong> blog post
已将 repmat
与 bsxfun
进行了基准测试,时间分别为@() A - repmat(mean(A),size(A,1),1)
与@() bsxfun(@minus,A,mean(A))
.如果需要涵盖所有内置基准测试,是否可以使用其他一些适用于浮点运算,关系运算和逻辑运算的比较模型?
Loren in her blog post
has benchmarked repmat
against bsxfun
with timing @() A - repmat(mean(A),size(A,1),1)
against @() bsxfun(@minus,A,mean(A))
respectively. If I need to cover benchmarking for all the built-ins, can I use some other comparison model that would work with floating point, relational and logical operations?
推荐答案
简介
关于bsxfun
是否优于repmat
或反之亦然的争论一直持续着.在本文中,我们将尝试比较MATLAB附带的各种内置程序如何在运行时性能上与repmat
等效项抗衡,并希望从中得出一些有意义的结论.
Introduction
The debate on whether bsxfun
is better than repmat
or vice versa has been going on like forever. In this post, we would try to compare how the different built-ins that ship with MATLAB fight it out against repmat
equivalents in terms of their runtime performances and hopefully draw some meaningful conclusions out of them.
如果从MATLAB环境或通过 Mathworks网站提取了官方文档,您可以看到bsxfun
支持的内置功能的完整列表.该列表具有用于浮点,关系和逻辑运算的功能.
If the official documentation is pulled out from the MATLAB environment or through the Mathworks website, one can see the complete list of built-in functions supported by bsxfun
. That list has functions for floating point, relational and logical operations.
在MATLAB 2015A
上,受支持的按元素的浮点运算是:
On MATLAB 2015A
, the supported element-wise floating point operations are :
- @plus(总和)
- @减(减)
- @times(乘法)
- @rdivide(右分隔)
- @ldivide(左分隔)
- @pow(电源)
- @rem(剩余)
- @mod(模数)
- @ atan2(四象限反切线)
- @ atan2d(四个象限的反切线,以度为单位)
- @hypot(平方和的平方根).
- @plus (summation)
- @minus (subtraction)
- @times (multiplication)
- @rdivide (right-divide)
- @ldivide (left-divide)
- @pow (power)
- @rem (remainder)
- @mod (modulus)
- @atan2 (four quadrant inverse tangent)
- @atan2d (four quadrant inverse tangent in degrees)
- @hypot (square root of sum of squares).
第二组包含按元素的关系操作,这些操作是:
The second set consists of element-wise relational operations and those are :
- @eq(等于)
- @ne(不等于)
- @lt(小于)
- @le(小于或等于)
- @gt(大于)
- @ge(大于或等于).
第三组也是最后一组包含以下所列的逻辑运算:
The third and final set comprises of logical operations as listed here :
- @and(逻辑与)
- @或(逻辑或)
- @xor(逻辑xor).
请注意,我们从比较测试中排除了两个内置的@max (maximum)
和@min (minimum)
,因为可能有很多方法可以实现它们的repmat
等效项.
Please note that we have excluded two built-ins @max (maximum)
and @min (minimum)
from our comparison tests, as there could be many ways one can implement their repmat
equivalents.
要真正比较repmat
和bsxfun
之间的性能,我们需要确保时序仅需要涵盖预期的操作.因此,像bsxfun(@minus,A,mean(A))
这样的东西将不是理想的,因为它必须在该bsxfun
调用内计算mean(A)
,但是计时可能并不重要.相反,我们可以使用大小与mean(A)
相同的另一个输入B
.
To truly compare the performances between repmat
and bsxfun
, we need to make sure that the timings only need to cover the operations intended. Thus, something like bsxfun(@minus,A,mean(A))
won't be ideal, as it has to calculate mean(A)
inside that bsxfun
call, however insignificant that timing might be. Instead, we can use another input B
of the same size as mean(A)
.
因此,我们可以使用:A = rand(m,n)
& B = rand(1,n)
,其中m
和n
是大小参数,我们可以更改它们并根据它们研究性能.正是在下一部分中列出的基准测试中完全做到了这一点.
Thus, we can use: A = rand(m,n)
& B = rand(1,n)
, where m
and n
are the size parameters which we could vary and study the performances based upon them. This is exactly done in our benchmarking tests listed in the next section.
用于这些输入的repmat
和bsxfun
版本看起来像这样-
The repmat
and bsxfun
versions to operate on those inputs would look something like these -
REPMAT: A + repmat(B,size(A,1),1)
BSXFUN: bsxfun(@plus,A,B)
基准化
最后,我们正处于这篇文章的重点,看着这两个家伙为之奋斗.我们将基准测试分为三组,一组用于浮点运算,另一组用于关系运算,第三组用于逻辑运算.如前所述,我们已经将比较模型扩展到所有这些操作.
Benchmarking
Finally, we are at the crux of this post to watch these two guys fight it out. We have segregated the benchmarking into three sets, one for the floating point operations, another for the relational and the third one for the logical operations. We have extended the comparison model as discussed earlier to all these operations.
Set1:浮点运算
这是使用repmat
和bsxfun
-
datasizes = [ 100 100; 100 1000; 100 10000; 100 100000;
1000 100; 1000 1000; 1000 10000;
10000 100; 10000 1000; 10000 10000;
100000 100; 100000 1000];
num_funcs = 11;
tsec_rep = NaN(size(datasizes,1),num_funcs);
tsec_bsx = NaN(size(datasizes,1),num_funcs);
for iter = 1:size(datasizes,1)
m = datasizes(iter,1);
n = datasizes(iter,2);
A = rand(m,n);
B = rand(1,n);
fcns_rep= {@() A + repmat(B,size(A,1),1),@() A - repmat(B,size(A,1),1),...
@() A .* repmat(B,size(A,1),1), @() A ./ repmat(B,size(A,1),1),...
@() A.\repmat(B,size(A,1),1), @() A .^ repmat(B,size(A,1),1),...
@() rem(A ,repmat(B,size(A,1),1)), @() mod(A,repmat(B,size(A,1),1)),...
@() atan2(A,repmat(B,size(A,1),1)),@() atan2d(A,repmat(B,size(A,1),1)),...
@() hypot( A , repmat(B,size(A,1),1) )};
fcns_bsx = {@() bsxfun(@plus,A,B), @() bsxfun(@minus,A,B), ...
@() bsxfun(@times,A,B),@() bsxfun(@rdivide,A,B),...
@() bsxfun(@ldivide,A,B), @() bsxfun(@power,A,B), ...
@() bsxfun(@rem,A,B), @() bsxfun(@mod,A,B), @() bsxfun(@atan2,A,B),...
@() bsxfun(@atan2d,A,B), @() bsxfun(@hypot,A,B)};
for k1 = 1:numel(fcns_bsx)
tsec_rep(iter,k1) = timeit(fcns_rep{k1});
tsec_bsx(iter,k1) = timeit(fcns_bsx{k1});
end
end
speedups = tsec_rep./tsec_bsx;
Set2:关系操作
时间关系操作的基准代码将用这些对应代码替换早期基准代码中的fcns_rep
和fcns_bsx
-
The benchmarking code to time relational operations would replace fcns_rep
and fcns_bsx
from the earlier benchmarking code with these counterparts -
fcns_rep = {
@() A == repmat(B,size(A,1),1), @() A ~= repmat(B,size(A,1),1),...
@() A < repmat(B,size(A,1),1), @() A <= repmat(B,size(A,1),1), ...
@() A > repmat(B,size(A,1),1), @() A >= repmat(B,size(A,1),1)};
fcns_bsx = {
@() bsxfun(@eq,A,B), @() bsxfun(@ne,A,B), @() bsxfun(@lt,A,B),...
@() bsxfun(@le,A,B), @() bsxfun(@gt,A,B), @() bsxfun(@ge,A,B)};
Set3:逻辑操作
最后一组基准测试代码将使用此处列出的逻辑运算-
The final set of benchmarking codes would use the logical operations as listed here -
fcns_rep = {
@() A & repmat(B,size(A,1),1), @() A | repmat(B,size(A,1),1), ...
@() xor(A,repmat(B,size(A,1),1))};
fcns_bsx = {
@() bsxfun(@and,A,B), @() bsxfun(@or,A,B), @() bsxfun(@xor,A,B)};
请注意,对于此特定集合,所需的输入数据A和B是逻辑数组.因此,我们必须在较早的基准测试代码中进行这些编辑才能创建逻辑数组-
Please note that for this specific set, the input data, A and B needed were logical arrays. So, we had to do these edits in the earlier benchmarking code to create logical arrays -
A = rand(m,n)>0.5;
B = rand(1,n)>0.5;
运行时和观察
基准测试代码是在以下系统配置上运行的:
Runtimes and Observations
The benchmarking codes were run on this system configuration:
MATLAB Version: 8.5.0.197613 (R2015a)
Operating System: Windows 7 Professional 64-bit
RAM: 16GB
CPU Model: Intel® Core i7-4790K @4.00GHz
运行基准测试后,用bsxfun
超过repmat
所获得的加速比被绘制为下图所示的三组.
The speedups thus obtained with bsxfun
over repmat
after running the benchmark tests were plotted for the three sets as shown next.
A.浮点运算:
从加速图中可以得出很少的观察结果:
Few observations could be drawn from the speedup plot:
-
atan2
和atan2d
明显是两个很好的bsxfun
加速案例. - 该列表中的下一个是在
repmat
等效代码上使用30% - 50%
升压执行的左右除法运算. - 在该列表中排在最下面的是其余的
7
操作,它们的加速似乎非常接近于统一,因此需要仔细检查.加速图可以缩小到那些7
操作,如下所示-
- The notably two good speedups cases with
bsxfun
are foratan2
andatan2d
. - Next in that list are right and left divide operations that boosts performs with
30% - 50%
over therepmat
equivalent codes. - Going further down in that list are the remaining
7
operations whose speedups seem very close to unity and thus need a closer inspection. The speedup plot could be narrowed down to just those7
operations as shown next -
根据上图,可以看到,除非使用@hypot
和@mod
的一次性案例,对于这些7
操作,bsxfun
的性能仍然比repmat
好大约10%.
Based on the above plot, one could see that barring one-off cases with @hypot
and @mod
, bsxfun
still performs roughly 10% better than repmat
for these 7
operations.
B.关系操作:
这是bsxfun
支持的下6个内置关系操作的第二组基准测试.
This is the second set of benchmarking for the next 6 built-in relational operations supported by bsxfun
.
看看上面的加速图,忽略了运行时间在bsxfun
和repmat
之间可比的起始情况,可以很容易地看到bsxfun
在这些关系运算中胜出.在将加速比触摸 10x
的情况下,对于这些情况,bsxfun
始终是首选.
Looking at the speedup plot above, neglecting the starting case that had comparable runtimes between bsxfun
and repmat
, one can easily see bsxfun
winning for these relational operations. With speedups touching 10x
, bsxfun
would always be preferable for these cases.
C.逻辑操作:
这是bsxfun
支持的其余3种内置逻辑操作的第三组基准测试.
This is the third set of benchmarking for the remaining 3 built-in logical operations supported by bsxfun
.
在开始时忽略了@xor
的一次性可比运行时情况,bsxfun
在这组逻辑运算中似乎也占了上风.
Neglecting the one-off comparable runtime case for @xor
at the start, bsxfun
seems to have an upper hand for this set of logical operations too.
- 在使用关系和逻辑运算时,很容易忘记
repmat
,而赞成使用bsxfun
.对于其余的情况,如果可以容忍5 - 7%
性能较低的情况,那么仍然可以坚持使用bsxfun
. - 看到与
bsxfun
一起使用关系和逻辑运算时性能会大大提高,因此可以考虑使用bsxfun
与ragged patterns
一起处理数据,例如用于单元格数组的数据.性能优势.我喜欢将这些解决方案案例称为使用bsxfun
的屏蔽功能的案例.这基本上意味着我们创建逻辑数组,即使用bsxfun
的掩码,可用于在单元格数组和数字数组之间交换数据.在数字数组中具有可用数据的优点之一是可以使用矢量化方法来处理它们.同样,由于bsxfun
是矢量化的一个很好的工具,您可能会发现自己再次使用它来解决相同的问题,因此有更多的理由来了解bsxfun
.为了读者的利益,这里很少有我能够探索此类方法的解决方案案例: 1 , 2 , 3 , 4 , 5 .
- When working with relational and logical operations,
repmat
could easily be forgotten in favor ofbsxfun
. For rest of the cases, one can still persist withbsxfun
if one off cases with5 - 7%
lesser performance is tolerable. - Seeing the kind of huge performance boost when using relational and logical operations with
bsxfun
, one can think about usingbsxfun
to work on data withragged patterns
, something like cell arrays for performance benefits. I like to call these solution cases as ones usingbsxfun
's masking capability. This basically means that we create logical arrays, i.e. masks withbsxfun
, which can be used to exchange data between cell arrays and numeric arrays. One of the advantages to have workable data in numeric arrays is that vectorized methods could be used to process them. Again, sincebsxfun
is a good tool for vectorization, you may find yourself using it once more working down on the same problem, so there are more reasons to get to knowbsxfun
. Few solution cases where I was able to explore such methods are linked here for the benefit of the readers: 1, 2, 3, 4, 5.
未来的工作
当前工作着重于使用repmat
沿一维复制数据.现在,repmat
可以沿多个维度进行复制,因此bsxfun
也可以进行扩展,其扩展等同于复制.因此,使用这两个功能对复制和扩展到多个维度执行类似的测试将很有趣.
Future work
The present work focused on replicating data along one dimension with repmat
. Now, repmat
can replicate along multiple dimensions and so do bsxfun
with its expansions being equivalent to replications. As such, it would be interesting to perform similar tests on replications and expansions onto multiple dimensions with these two functions.
这篇关于比较BSXFUN和REPMAT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!