这种观察并不那么重要,因为浪费在循环语句上的时间性能可能比循环本身要高得多。但是无论如何,我将共享它,因为我进行了搜索,但找不到与此相关的主题。我总是有这样的印象:预分配要循环的数组,然后在其上循环,比直接在其上循环并决定检查它要好。代码将是比较这两个方面的效率:

disp('Pure for with column on statement:')
tic
for k=1:N
end
toc

disp('Pure for with column declared before statement:')
tic
m=1:N;
for k=m
end
toc

但是我得到的结果是:
Pure for with column on statement:
Elapsed time is 0.003309 seconds.
Pure for with column declared before statement:
Elapsed time is 0.208744 seconds.

为什么会这样呢?预分配不应该更快吗?

实际上,matlab help for表示:



因此,与我的期望相矛盾,for语句中的列表达式更好,因为它不分配向量,因此,更快。

我编写了以下脚本来测试其他情况,我也认为这样做会更快:
% For comparison:
N=1000000;

disp('Pure for loop on cell declared on statement:')
tic
for k=repmat({1},1,N)
end
toc

disp('Pure for loop on cell declared before statement:')
tic
mcell=repmat({1},1,N);
for k=mcell
end
toc

disp('Pure for loop calculating length on statement:')
tic
for k=1:length(mcell)
end
toc

disp('Pure for loop calculating length before statement:')
tic
lMcell = length(mcell);
for k=1:lMcell
end
toc

disp('Pure while loop using le:')
% While comparison:
tic
k=1;
while (k<=N)
  k=k+1;
end
toc

disp('Pure while loop using lt+1:')
% While comparison:
tic
k=1;
while (k<N+1)
  k=k+1;
end
toc


disp('Pure while loop using lt+1 pre allocated:')
tic
k=1;
myComp = N+1;
while (k<myComp)
  k=k+1;
end
toc

时间是:
Pure for loop on cell declared on statement:
Elapsed time is 0.259250 seconds.
Pure for loop on cell declared before statement:
Elapsed time is 0.260368 seconds.
Pure for loop calculating length on statement:
Elapsed time is 0.012132 seconds.
Pure for loop calculating length before statement:
Elapsed time is 0.003027 seconds.
Pure while loop using le:
Elapsed time is 0.005679 seconds.
Pure while loop using lt+1:
Elapsed time is 0.006433 seconds.
Pure while loop using lt+1 pre allocated:
Elapsed time is 0.005664 seconds.

结论:
  • 您可以仅通过在逗号语句上循环来获得一些性能,但是与在for循环上花费的时间相比可以忽略不计。
  • 对于细胞,差异似乎可以忽略不计。
  • 最好在执行循环之前预先分配长度。
  • 在不预先分配向量的情况下,while具有与for相同的效率,这在
  • 之前已经说过了
  • 如预期的那样,最好在while语句之前计算固定表达式。

  • 但是我无法回答的问题是,电池如何处理,为什么没有时差?开销可能比观察到的要少得多吗?还是因为它不是 double 类型的基本类型而必须分配单元格?

    如果您知道有关此主题的其他技巧,则可以免费添加。

    只需添加时间以显示转@@ Magla的答案中所述的feature('accel','off')的结果即可。
    Pure for with column on statement:
    Elapsed time is 0.181592 seconds.
    Pure for with column declared before statement:
    Elapsed time is 0.180011 seconds.
    Pure for loop on cell declared on statement:
    Elapsed time is 0.242995 seconds.
    Pure for loop on cell declared before statement:
    Elapsed time is 0.228705 seconds.
    Pure for loop calculating length on statement:
    Elapsed time is 0.178931 seconds.
    Pure for loop calculating length before statement:
    Elapsed time is 0.178486 seconds.
    Pure while loop using le:
    Elapsed time is 1.138081 seconds.
    Pure while loop using lt+1:
    Elapsed time is 1.241420 seconds.
    Pure while loop using lt+1 pre allocated:
    Elapsed time is 1.162546 seconds.
    

    现在的结果符合预期…

    最佳答案

    这一发现与是否进行预分配无关:它处理了启用或不使用多个内核计算事物的matlab。当您在for语句中插入冒号运算符时,它告诉matlab使用多个内核(即多线程)。

    如果仅使用feature('accel','off')在一个内核上设置matlab,则与doubles的观察到的差异将消失。关于cells,matlab不使用多线程-因此无法观察到任何差异(无论accel的状态如何)。

    当使用冒号时,且仅当使用冒号时,for循环才是多线程的。以下具有相似长度的向量不涉及多个核心:

  • for k = randperm(N)
  • for k = linspace(1,N,N)

  • 但是for k = 1:0.9999:N是多线程的。

    可以在此matlab's support page上找到一种解释。它指出,当“该函数执行的算法中的运算很容易划分为可以同时执行的部分”时,可以完成多核处理。使用冒号运算符,Matlab知道可以对for进行分区。

    10-07 14:46