performance - 为什么在 Matlab 中 .* 运算符在某些情况下比 * 对于标量更快?

标签 performance matlab

考虑以下代码:

a=rand(10000); b=rand(10000);
tic; 2*(a<b); toc;
tic; 2.*(a<b); toc;    

结果是:

Elapsed time is 0.938957 seconds.
Elapsed time is 0.426517 seconds.

为什么第二种情况比第一种情况快两倍?

编辑: 我用任何大小的矩阵得到相同的结果,无论你测试它的顺序如何,用

(a<b).*3.56 vs (a<b)*3.56

例如,但不是

(a.*b)*2 vs (a.*b).*2

(a*b)*2 vs (a*b).*2

好像和逻辑数组有联系,因为我的结果和

(a&b)*2 vs (a&b).*2

计算机:R2015b,Windows 10 x64

最佳答案

我建议对性能进行更严格的检查。将您的测试放在一个命名函数中,让 MATLAB 优化这两段代码,并多次运行两段代码,选择最快的运行时间。我的预感是它们应该花费相同的时间,尽管我现在无法使用合理的矩阵大小进行检查。这是我要做的:

function product_timing(N)

a=rand(N);
b=rand(N);

tmin=inf;
for k=1:10
    tic;
    res1=2*(a<b);
    t=toc;
    
    if t<tmin
        tmin=t;
    end
end

disp(tmin);


tmin=inf;
for k=1:10
    tic;
    res2=2.*(a<b);
    t=toc;
    
    if t<tmin
        tmin=t;
    end
end

更新

在我的 R2012b 上,这两种方法之间似乎没有明显区别。然而,正如其他人所指出的,R2015b 及其新的执行引擎让一切变得不同。

虽然我仍然不确定答案,但让我收集来自@x1hgg1x的反馈(对此答案和问题的评论)和 @LuisMendo ( in chat ),只是为了说明我的无知:

  • c*3.56是比 c.*3.56 慢的整数倍(线程数?) (使用任何标量)if clogical , 但不是如果 cuint8double
  • 这同样适用于向量,而不仅仅是方阵

如前所述on a MATLAB product page :

Run your programs faster with the redesigned MATLAB® execution engine.

The improved architecture uses just-in-time (JIT) compilation of all MATLAB code with a single execution pathway. The engine offers improved language quality and provides a platform for future enhancements.

Specific performance improvements include those made to:

...

Element-Wise Math Operations

The execution of many element-wise math operations is optimized. These operations are element-by-element arithmetic operations on arrays such as the following:

>> b = ((a+1).*a)./(5-a);

但是,查看 .* 的文档和 * ,我看不到太多与该问题相关的信息。来自 array vs matrix operations 的注释关于像 .* 这样的数组操作:

If one operand is a scalar and the other is not, then MATLAB applies the scalar to every element of the other operand. This property is known as scalar expansion because the scalar expands into an array of the same size as the other input, then the operation executes as it normally does with two arrays.

还有 doc of the matrix product *

If at least one input is scalar, then A*B is equivalent to A.*B and is commutative.

正如我们所见,A*B等价A.*B是有争议的。好吧,它们在数学上是等价的,但有些奇怪的事情正在发生。

由于上述注意事项,以及性能差异仅出现在 logical 上这一事实数组,我会认为这是一个未记录的功能。我会认为它与 logical 有关s 每个只占用 1 个字节,但加速不体现为 uint8阵列。我建议自 logical s 实际上包含单个位的信息,一些内部优化是可能的。这仍然不能解释为什么 mtimes不会这样做,这肯定与 times 的内部运作有关对比mtimes .

有一件事是肯定的:times实际上并没有依赖于 mtimes对于标量操作数(也许应该?)。由于在 R2012b 中缺少整个效果,我相信上面提到的新执行引擎的优化数组操作会单独处理逻辑数组,从而允许 scalar.*logical_array 的特殊情况。以加快速度,但 mtimes 中缺少相同的优化.

关于performance - 为什么在 Matlab 中 .* 运算符在某些情况下比 * 对于标量更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34832279/

相关文章:

matlab - K-means 在某些图像区域比高斯混合模型更准确

c# - 3D View 中的 WPF 2D 动画 : Performance issue

c - sse2 浮点乘法

performance - 寻找更快的方法来处理细胞和矢量操作

arrays - 如果数组末尾的额外元素在嵌套循环的下一次迭代中更短,如何计算数组末尾的额外元素

matlab - 如何将数字结果转换为符号或字符串?

matlab - 使用标识符索引单元格

Matlab:用3个向量制作等高线图

database - 数据库的选择

c# - 从月份数组中获取一系列值