正如Steve Eddins提出的commented一样,对于小的数组,implicit expansion (在Matlab R2016b中引入)比bsxfun
更快,而对于大的数组,它的速度与bsxfun
相似:
在R2016b中,隐式扩展在大多数情况下与bsxfun一样快,甚至更快。隐式扩展的最佳性能增益是使用较小的矩阵和数组大小。对于较大的矩阵大小,隐式扩展的速度往往与
bsxfun
大致相同。
此外,发生扩展的维度可能会产生影响:
当第一维中存在扩展时,运算符可能没有bsxfun
那么快。
(感谢@Poelie和@rayryeng让我对此进行know about!)
自然会出现两个问题:
与bsxfun
的区别是数组大小或形状是什么
发布于 2017-03-02 23:50:07
为了测量速度的差异,已经进行了一些测试。测试考虑两种不同的操作
和要操作的数组的四种不同形状的:
带
N×N
阵列的arrayN×N×N×N
阵列带N×1×N
arrayN×N×N×N
阵列的1×N
阵列带1×N×N
阵列对于运算和数组形状的八种组合中的每一种,都使用隐式扩展和bsxfun
进行相同的运算。使用了 N
的几个值,以涵盖从小到大的数组。timeit
用于可靠的计时。
本答案末尾给出了基准测试代码。它已经在R2016b,Windows10上运行,内存为12 GB。
结果
下图显示了结果。水平轴是输出数组的元素数,这是比N
更好的大小度量。
还使用逻辑运算(而不是算术运算)进行了测试。为了简洁起见,这里没有显示结果,但显示了类似的趋势。
结论
根据图表:
bsxfun
相似,似乎没有太大的影响,至少在所考虑的情况下是这样。timeit
对于小尺寸并不准确,因为代码太快(实际上,它会对这种小尺寸发出警告)。1e5
左右时,两种速度变得相等。此值可以为system-dependent.基准测试代码
clear
% NxN, Nx1, addition / power
N1 = 2.^(4:1:12);
t1_bsxfun_add = NaN(size(N1));
t1_implicit_add = NaN(size(N1));
t1_bsxfun_pow = NaN(size(N1));
t1_implicit_pow = NaN(size(N1));
for k = 1:numel(N1)
N = N1(k);
x = randn(N,N);
y = randn(N,1);
% y = randn(1,N); % use this line or the preceding one
t1_bsxfun_add(k) = timeit(@() bsxfun(@plus, x, y));
t1_implicit_add(k) = timeit(@() x+y);
t1_bsxfun_pow(k) = timeit(@() bsxfun(@power, x, y));
t1_implicit_pow(k) = timeit(@() x.^y);
end
% NxNxNxN, Nx1xN, addition / power
N2 = round(sqrt(N1));
t2_bsxfun_add = NaN(size(N2));
t2_implicit_add = NaN(size(N2));
t2_bsxfun_pow = NaN(size(N2));
t2_implicit_pow = NaN(size(N2));
for k = 1:numel(N1)
N = N2(k);
x = randn(N,N,N,N);
y = randn(N,1,N);
% y = randn(1,N,N); % use this line or the preceding one
t2_bsxfun_add(k) = timeit(@() bsxfun(@plus, x, y));
t2_implicit_add(k) = timeit(@() x+y);
t2_bsxfun_pow(k) = timeit(@() bsxfun(@power, x, y));
t2_implicit_pow(k) = timeit(@() x.^y);
end
% Plots
figure
colors = get(gca,'ColorOrder');
subplot(121)
title('N\times{}N, N\times{}1')
% title('N\times{}N, 1\times{}N') % this or the preceding
set(gca,'XScale', 'log', 'YScale', 'log')
hold on
grid on
loglog(N1.^2, t1_bsxfun_add, 's-', 'color', colors(1,:))
loglog(N1.^2, t1_implicit_add, 's-', 'color', colors(2,:))
loglog(N1.^2, t1_bsxfun_pow, '^-', 'color', colors(1,:))
loglog(N1.^2, t1_implicit_pow, '^-', 'color', colors(2,:))
legend('Addition, bsxfun', 'Addition, implicit', 'Power, bsxfun', 'Power, implicit')
subplot(122)
title('N\times{}N\times{}N{}\times{}N, N\times{}1\times{}N')
% title('N\times{}N\times{}N{}\times{}N, 1\times{}N\times{}N') % this or the preceding
set(gca,'XScale', 'log', 'YScale', 'log')
hold on
grid on
loglog(N2.^4, t2_bsxfun_add, 's-', 'color', colors(1,:))
loglog(N2.^4, t2_implicit_add, 's-', 'color', colors(2,:))
loglog(N2.^4, t2_bsxfun_pow, '^-', 'color', colors(1,:))
loglog(N2.^4, t2_implicit_pow, '^-', 'color', colors(2,:))
legend('Addition, bsxfun', 'Addition, implicit', 'Power, bsxfun', 'Power, implicit')
https://stackoverflow.com/questions/42559922
复制相似问题