[英]matlab sum(X-Y) vs sum(X) - sum(Y)
If we have two matrices X
and Y
, both two-dimensional, now mathematically we can say: sum(XY)=sum(X)-sum(Y)
. 如果我们有两个矩阵
X
和Y
,两个都是二维的,现在在数学上我们可以说: sum(XY)=sum(X)-sum(Y)
。
Which is more efficient in Matlab? 哪个在Matlab中更有效? Which is faster?
哪个更快?
On my machine, sum(xy)
is slightly faster for small arrays, but sum(x)-sum(y)
is quite a lot faster for larger arrays. 在我的机器上,对于小型数组,
sum(xy)
略快一些,但对于较大的数组, sum(x)-sum(y)
要快得多。 To benchmark, I'm using MATLAB R2015a on a Windows 7 machine with 32GB memory. 为了进行基准测试,我在具有32GB内存的Windows 7机器上使用MATLAB R2015a。
n = ceil(logspace(0,4,25));
for i = 1:numel(n)
x = rand(n(i));
y = rand(n(i));
t1(i) = timeit(@()sum(x-y));
t2(i) = timeit(@()sum(x)-sum(y));
clear x y
end
figure; hold on
plot(n, t1)
plot(n, t2)
legend({'sum(x-y)', 'sum(x)-sum(y)'})
xlabel('n'); ylabel('time')
set(gca, 'XScale', 'log', 'YScale', 'log')
You got me all curious and I decided to run some benchmark. 你让我很好奇,我决定运行一些基准测试。 By the time I was done it seems knedlsepp had it right as for larger matrices
sum(XY)
become quite slower. 当我完成时,似乎knedlsepp正确,因为较大的矩阵
sum(XY)
变得相当慢。
The crossover seems to happen around 10^3
elements. 交叉似乎发生在
10^3
元素周围。
%% // Benchmark code
nElem = (1:9).'*(10.^(1:6)) ; nElem = nElem(:) ; %'//damn pretifier
nIter = numel(nElem) ;
res = zeros(nIter,2) ;
for ii=1:nIter
X = rand(nElem(ii) ,1) ;
Y = rand(nElem(ii) ,1) ;
f1 = @() sum(X-Y) ;
f2 = @() sum(X)-sum(Y) ;
res(ii,1) = timeit( f1 ) ;
res(ii,2) = timeit( f2 ) ;
clear f1 f2 X Y
end
loglog(nElem,res,'DisplayName','nElem')
I ran that a few times and the results are quite consistent on my machine. 我跑了几次,结果在我的机器上非常一致。 I blew my memory trying to go above 10^7 elements.
我试图超过10 ^ 7个元素,让自己记忆犹新。 Feel free to extend the test but I don't think the relationship is going to change much.
随意扩展测试,但我认为这种关系不会发生太大变化。
Specs: Windows 8.1 Pro / Matlab R2013a on: 规格:Windows 8.1 Pro / Matlab R2013a上:
Assuming that both x and y have N x M = K elements, then For sum(x)-sum(y) you have: 假设x和y都有N x M = K个元素,那么对于sum(x)-sum(y),你有:
K memory access to read x, K memory access to read y, one memory access to write the result --> 2K + 1 memory access (Assuming that the intermediate sum inside the sum function will be held in a CPU register). K内存访问读取x,K内存访问读取y,一次内存访问写入结果 - > 2K + 1内存访问(假设sum函数内的中间和将保存在CPU寄存器中)。
2K sum operations + 1 subtraction --> 2k + 1 CPU operations. 2K求和运算+ 1减法 - > 2k + 1 CPU运算。
For sum(x - y): Matlab will calculate x - y and store the output then calculate the sum, so we have K memory access to read x, K memory access to read y, K memory access to write the result of the subtraction, K memory access to read the subtraction result again for the sum function then 1 memory access to write the sum result --> 4K + 1 memory operations. 对于sum(x - y):Matlab将计算x-y并存储输出然后计算总和,所以我们有K个存储器访问读取x,K个存储器访问读取y,K个存储器访问写入减法结果,K内存访问再次读取减法结果为sum函数然后1内存访问写入和结果 - > 4K + 1内存操作。
k subtractions + k summations --> 2K operations. k减法+ k求和 - > 2K运算。
As we can see sum(x - y) consumes many memory access, so in large number of elements it may consume higher time but I don't have an explanation why it's faster for small number of elements. 正如我们所看到的,sum(x - y)消耗了许多内存访问,因此在大量元素中它可能会消耗更多的时间,但我没有解释为什么它对于少量元素来说更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.