A==0真的比~A好吗? [英] Is A==0 really better than ~A?

查看:32
本文介绍了A==0真的比~A好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题设置简介

我正在做一些基准测试,涉及 - ~AA==0 对于 没有 NaNs 的双数组,两者都转换A 到一个逻辑数组,其中所有 zeros 都转换为 true 值,其余设置为 false 值.

I was doing some benchmarks involving - ~A and A==0for a double array with no NaNs, both of which convert A to a logical array where all zeros are converted to true values and rest are set as false values.

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

For the benchmarking, I have used three sets of input data –

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

输入是用 A = round(rand(N)*20) 创建的,其中 N 是从大小数组中获取的参数.因此,对于第一组,N 将在 15 到 100 之间变化,步长为 5,第二组和第三组类似.请注意,我将 datasize 定义为 N,因此元素数将为 datasize^2 或 N^2.

The input is created with A = round(rand(N)*20), where N is the parameter taken from the size array. Thus, N would vary from 15 to 100 with stepsize of 5 for the first set and similarly for the second and third sets. Please note that I am defining datasize as N, thus the number of elements would be datasize^2 or 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

结果

最后是问题

可以看到 A==0 在所有数据大小上的表现如何优于 ~A.所以这里有一些观察和相关的问题——

One can see how A==0 performs better than ~A across all datasizes. So here are some observations and related questions alongside them –

  1. A==0 有一个关系运算符和一个操作数,而 ~A 只有一个关系运算符.两者都产生逻辑数组并且都接受双数组.事实上,A==0 也适用于 NaNs,而 ~A 不行.那么,为什么仍然 ~A 至少不如 A==0 好,因为看起来 A==0 正在做更多的工作还是我在这里遗漏了什么?

  1. A==0 has one relational operator and one operand, whereas ~A has only one relational operator. Both produce logical arrays and both accept double arrays. In fact, A==0 would work with NaNs too, wheras ~A won’t. So, why is still ~A at least not as good as A==0 as it looks like A==0 is doing more work or am I missing something here?

A==0 的运行时间有一个特殊的下降,因此在 N = 320,即 102400 处提高了性能> A 的元素.我在我可以访问的两个不同系统上以这种大小进行的多次运行中观察到了这一点.那么那里发生了什么?

There’s a peculiar drop of elapsed time with A==0 and thus increased performance at N = 320, i.e. at 102400 elements for A. I have observed this across many runs with that size on two different systems that I have access to. So what’s going on there?

推荐答案

严格来说这不是一个答案,而是我对讨论的贡献

我使用 profiler 来调查你的代码的一个稍微修改的版本:

I used the profiler to investigate a slightly-modified version of your code:

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

我使用了以下分析器标志(根据 UndocumentedMatlab 关于 Profiler 的系列帖子):

I used the following profiler flags (as per UndocumentedMatlab's series of posts about Profiler):

profile('-memory','on');
profile('on','-detail','builtin');

这里是分析器结果的摘录(大图链接):

And here's an excerpt from the profiler results (link to the larger image):

似乎 == 变体分配了一点额外的内存,让它发挥它的魔力......

It seems that the == variant allocates a tiny bit of additional memory that allows it to work its magic....

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

Regarding your question 2: Before removing the keeping of timeall, I tried plotting the same charts you did in Excel. I didn't observe the behavior you mentioned for N = 320. I suspect this may have something to do with the additional wrappers (i.e. function handles) you're using in your code.

我想我会附上所讨论函数的可用文档以供快速参考.

I thought I'd attach the available documentation for the discussed functions for quick reference.

~ (\MATLAB\R20???\toolbox\matlab\ops\not.m) 的文档:

The documentation for ~ (\MATLAB\R20???\toolbox\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.

== (\MATLAB\R20???\toolbox\matlab\ops\eq.m) 的文档:

The documentation for == (\MATLAB\R20???\toolbox\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.

这篇关于A==0真的比~A好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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