A == 0真的比~A好吗?

Div*_*kar 25 arrays performance benchmarking matlab logical-operators

问题设置简介

我在做一些涉及基准- ~AA==0double array with no NaNs,两者都转换A为逻辑阵列,所有zeros被转换为true值和休息被设置为false值.

对于基准测试,我使用了三组输入数据 -

  • 从小到小的数据 - 15:5:100
  • 中小型数据 - 50:40:1000
  • 中型到大型数据 - 200:400:3800

输入是使用A = round(rand(N)*20),其中N是从size数组中获取的参数.因此,N会从变化15 to 100 with stepsize of 5为所述第一组和类似地对于第二和第三组.请注意,我将datasize定义为N,因此元素的数量将是datasize ^ 2或N ^ 2.

基准代码

N_arr = 15:5:100; %// for very small to small sized input array
N_arr = 50:40:1000; %// for small to medium sized input array
N_arr = 200:400:3800; %// for medium to large sized input array
timeall = zeros(2,numel(N_arr));
for k1 = 1:numel(N_arr)
    A = round(rand(N_arr(k1))*20);

    f = @() ~A;
    timeall(1,k1) = timeit(f);
    clear f

    f = @() A==0;
    timeall(2,k1) = timeit(f);
    clear f
end
Run Code Online (Sandbox Code Playgroud)

结果

在此输入图像描述

在此输入图像描述

在此输入图像描述

最后问题

人们可以看到A==0~A所有数据集更好的表现.所以这里有一些观察和相关问题 -

  1. A==0有一个关系运算符和一个操作数,而~A只有一个关系运算符.两者都产生逻辑数组并且都接受双数组.事实上,A==0也会合作NaNs,而~A不会.那么,为什么~A至少仍然没有A==0看起来像A==0做更多的工作一样好,或者我错过了什么?

  2. 随着时间的流逝,特别是在A的元素A==0处有一个特殊的下降时间,从而提高了性能.我已经在我可以访问的两个不同系统上的许多运行中观察到了这种情况.那么那里发生了什么?N = 320102400

Dev*_*-iL 5

这不仅仅是答案,而是我对讨论的贡献

我用它profiler来调查代码的略微修改版本:

N_arr = 200:400:3800; %// for medium to large sized input array

for k1 = 1:numel(N_arr)

    A = randi(1,N_arr(k1));
    [~]=eq(A,0);
    clear A

    A = randi(1,N_arr(k1));
    [~]=not(A);
    clear A   

end
Run Code Online (Sandbox Code Playgroud)

我使用了以下的探查器标志(根据UndocumentedMatlab的一系列帖子Profiler):

profile('-memory','on');
profile('on','-detail','builtin');
Run Code Online (Sandbox Code Playgroud)

这里是剖析器结果的摘录(链接到更大的图像): Profiler输出

似乎该==变体分配了一小部分额外的内存,使其能够发挥其魔力....

关于你的问题2:在删除保留之前timeall,我尝试绘制你在Excel中所做的相同图表.我没有观察到你提到的行为N = 320.我怀疑这可能与您在代码中使用的其他包装器(即函数句柄)有关.


我想我会附上所讨论功能的可用文档,以便快速参考.

对于文档~(\ MATLAB\R20 ??? \工具箱\ MATLAB\OPS \not.m):

%~   Logical NOT.
%   ~A performs a logical NOT of input array A, and returns an array
%   containing elements set to either logical 1 (TRUE) or logical 0 (FALSE).
%   An element of the output array is set to 1 if A contains a zero value
%   element at that same array location.  Otherwise, that element is set to
%   0.
%
%   B = NOT(A) is called for the syntax '~A' when A is an object.
%
%   ~ can also be used to ignore input arguments in a function definition,
%   and output arguments in a function call.  See "help punct"

%   Copyright 1984-2005 The MathWorks, Inc.
Run Code Online (Sandbox Code Playgroud)

对于文档==(\ MATLAB\R20 ??? \工具箱\ MATLAB\OPS\eq.m):

%==  Equal.
%   A == B does element by element comparisons between A and B
%   and returns a matrix of the same size with elements set to logical 1
%   where the relation is true and elements set to logical 0 where it is
%   not.  A and B must have the same dimensions unless one is a
%   scalar. A scalar can be compared with any size array.
%
%   C = EQ(A,B) is called for the syntax 'A == B' when A or B is an
%   object.

%   Copyright 1984-2005 The MathWorks, Inc.
Run Code Online (Sandbox Code Playgroud)