最快的方法来总结矩阵的元素

时间:2016-04-06 09:23:37

标签: matlab sum processing-efficiency memory-efficient space-efficiency

我的代码效率有些问题。基本上我的代码就像这样:

a = zeros(1,50000);
for n = 1:50000
    a(n) = 10.*n - 5;
end
sum(a);

解决此矩阵所有元素之和的最快方法是什么?

1 个答案:

答案 0 :(得分:3)

首先你要通过使它成为向量乘法来删除你的for循环:

tic
a = zeros(1,50000);
b = [1:50000];
   a = 10.*b-5;
result = sum(a);
toc
Elapsed time is 0.008504 seconds.

另一种方法是简化您的操作,您将1乘以50000乘以10然后减去5然后取总和(这是一个数字),这相当于:

tic
result = sum(1:50000)*10 - 5*50000;
toc
Elapsed time is 0.003851 seconds.

或者如果你真的喜欢数学(这是一种纯粹的数学表达方法):

tic
result = (1+50000)*(50000/2)*10 - 5*50000;
toc
Elapsed time is 0.003702 seconds.

正如你所看到的,一点点数学可以比纯粹的高效编程做得更好,实际上,循环并不总是很慢,在你的情况下,循环实际上比矢量化方法更快:

tic
a = zeros(1,50000);
  for n = 1:50000
     a(n)=10.*n-5;
  end
sum(a);
toc
Elapsed time is 0.006431 seconds.

时序

让我们做一些时间并看看结果。底部提供了自己运行它的功能。大致执行时间execTime以秒为单位,改进百分比impPercentage以%为单位。

结果

  • OSX 10.11.4上的R2016a

                   execTime     impPercentage
                  __________    _____________
    loop          0.00059336         0       
    vectorized    0.00014494    75.574       
    adiel         0.00010468    82.359       
    math          9.3659e-08    99.984       
    

代码

以下函数可用于生成输出。请注意,它需要最少的 R2013b 才能使用内置的timeit - 函数和table

function timings
%feature('accel','on')      %// commented out because it's undocumented
cycleCount = 100;
execTime = zeros(4,cycleCount);
names = {'loop';'vectorized';'adiel';'math'};
w = warning;
warning('off','MATLAB:timeit:HighOverhead');
for k = 1:cycleCount
    execTime(1,k) = timeit(@()loop,1);
    execTime(2,k) = timeit(@()vectorized,1);
    execTime(3,k) = timeit(@()adiel,1);
    execTime(4,k) = timeit(@()math,1);
end
warning(w);
execTime = min(execTime,[],2);
impPercentage = (1 - execTime/max(execTime)) * 100;
table(execTime,impPercentage,'RowNames',names)


function result = loop
a = zeros(1,50000);
for n = 1:50000
    a(n) = 10.*n - 5;
end
result = sum(a);

function result = vectorized
b = 1:50000;
a = 10.*b - 5;
result = sum(a);

function result = adiel
result = sum(1:50000)*10 - 5*50000;

function result = math
result = (1+50000)*(50000/2)*10 - 5*50000;