这个观察并不那么重要,因为循环语句所浪费的时间性能可能会比循环本身高得多。但无论如何,我会分享它,因为我搜索并找不到关于此的主题。我一直有这样的印象,预分配我要循环的数组,然后在上面循环,会比直接循环更好,并决定检查它。该代码将比较这两个 fors 之间的效率:
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.
为什么会这样?预分配不应该更快吗?
事实上,matlabhelp for
说:
当 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 语句之前计算固定表达式。
但我无法回答的问题是,细胞呢,为什么没有时差?开销可能比观察到的要小得多?或者它必须分配单元格,因为它不是双精度类型的基本类型?
如果您知道有关此主题的其他技巧,请随意添加。
只需添加时间以显示转动的结果,feature('accel','off')
如@Magla 的回答中所述。
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.
现在的结果符合预期……</p>