在我的程序中,我需要计算总和:
.
C
我用和的新值计算了这个总和 2500 次z
。
参数z可以是一个向量。我编写了简单的 for 循环和矢量化版本代码,如下所示:
K = 200;
n_z = 40000;
C = ones(K,1); % an example, in real life the arey some coefficients (at next call will be new)
k = 0:K-1;
z = linspace(0, 2*pi, n_z); % at next call will be new
tic;
my_sum_for = zeros(1, K);
for i=1:n_z
my_sum_for(i) = C' * tan(k' * z(i));
end
toc; % Elapsed time is 1.820485 seconds.
tic;
my_sum = C' * tan(k' * z);
toc; % Elapsed time is 0.160924 seconds.
矢量化版本更快,但还不够。是否可以改进矢量化版本?
在 Dominique Jacquel 的回答之后,我有了这个矢量化版本,它更快:
K = 200;
n_z = 40000;
C = ones(K,1)'; % an example, in real life they are some coefficients (at next call will be new)
k = (0:K-1)';
z = linspace(0, 2*pi, n_z); % at next call will be new
tic;
my_sum_for = zeros(1, K);
for i=1:n_z
my_sum_for(i) = C * tan(k * z(i));
end
toc; % Elapsed time is 1.521587 seconds.
tic;
my_sum = C * tan(k * z);
toc; % Elapsed time is 0.125468 seconds.
是否可以进一步改进矢量化版本(bsxfun、arrayfun 之类的)?250 秒的时间对我来说仍然很慢(占所有计算的 75%)。