Matlab性能:比较慢于算术 [英] Matlab performance: comparison slower than arithmetic

查看:93
本文介绍了Matlab性能:比较慢于算术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前一段时间,我为这个问题.

目标:计算此矩阵中[3 6]范围内的值的数量:

Objective: count the number of values in this matrix that are in the [3 6] range:

A = [2 3 4 5 6 7;
     7 6 5 4 3 2]

我想出了12种不同的方法:

I came up with 12 different ways to do it:

count = numel(A( A(:)>3 & A(:)<6 ))      %# (1)
count = length(A( A(:)>3 & A(:)<6 ))     %# (2)
count = nnz( A(:)>3 & A(:)<6 )           %# (3)
count = sum( A(:)>3 & A(:)<6 )           %# (4)

Ac = A(:);
count = numel(A( Ac>3 & Ac<6 ))          %# (5,6,7,8)
%# prevents double expansion
%# similar for length(), nnz(), sum(),
%# in the same order as (1)-(4)

count = numel(A( abs(A-(6+3)/2)<3/2 ))   %# (9,10,11,12)
%# prevents double comparison and & 
%# similar for length(), nnz(), sum()
%# in the same order as (1)-(4)

因此,我决定找出最快的.测试代码:

So, I decided to find out which is fastest. Test code:

A = randi(10, 50);
tic
for ii = 1:1e5

    %# method is inserted here

end
toc

结果(最好的5次运行,都在几秒钟之内):

results (best of 5 runs, all in seconds):

%# ( 1): 2.981446
%# ( 2): 3.006602
%# ( 3): 3.077083
%# ( 4): 2.619057
%# ( 5): 3.011029
%# ( 6): 2.868021
%# ( 7): 3.149641
%# ( 8): 2.457988
%# ( 9): 1.675575
%# (10): 1.675384
%# (11): 2.442607
%# (12): 1.222510

所以看来count = sum(( abs(A(:)-(6+3)/2) < (3/2) ));是到达这里最快的方法...

So it seems that count = sum(( abs(A(:)-(6+3)/2) < (3/2) )); is the fastest way to go here...

我将一个<分为两个部分,一个加法和一个abs进行交易,执行时间不到一半!有人对此有一个解释吗?

I trade one < with two divisions, an addition and an abs, and the execution time is less than half! Does anyone have an explanation for why this is?

JIT编译器可能用内存中的单个值替换了除法/加法,但是还有abs ...分支错误预测呢?像这样简单的事情似乎很愚蠢...

The JIT compiler probably replaces the divisions/additions with a single value in memory, but there's still the abs...Branch misprediction then? Seems silly for something as simple as this...

推荐答案

A(:)>3 & A(:)<6表达式需要评估两个条件,而abs(A(:)-(6+3)/2) < 3/2)表达式仅评估一个条件.

The A(:)>3 & A(:)<6 expression needs to evaluate two conditions, whereas the abs(A(:)-(6+3)/2) < 3/2) evaluates one only one.

对于非常紧密的计算密集型循环,这有很大的不同.即使没有分支错误的预测,分支本身也是相对昂贵的.因此,例如 循环展开 可以用作优化技术的原因

For very tight compute intensive loops this makes a lot of difference. Even without branch mispredictions, branching in itself is relatively costly. That's why, for instance, loop unrolling works as an optimization technique.

这篇关于Matlab性能:比较慢于算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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