gpt4 book ai didi

performance - matlab sum(X-Y) vs sum(X) - sum(Y)

转载 作者:太空宇宙 更新时间:2023-11-03 20:09:31 32 4
gpt4 key购买 nike

如果我们有两个矩阵 XY,都是二维的,现在在数学上我们可以说:sum(X-Y)=sum(X)-总和(Y).

Matlab 哪个效率更高?哪个更快?

最佳答案

在我的机器上,sum(x-y) 对于小数组稍微快一些,但是 sum(x)-sum(y) 对于大数组快很多.为了进行基准测试,我在具有 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')

enter image description here

关于performance - matlab sum(X-Y) vs sum(X) - sum(Y),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30060154/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com