tic;
i=1:1000000;
s=sum(i)
disp('use sum');toc
tic;s=0;
for i=1:1000000
s=s+i;
end
s
disp('use for');toc
tic;s=0;i=0;
while i<1000000
s=s+i;
i=i+1;
end
s
disp('use while');toc
结果
s =
5.0000e+011
use sum
Elapsed time is 0.046000 seconds.
s =
5.0000e+011
use for
Elapsed time is 0.016000 seconds.
s =
5.0000e+011
use while
Elapsed time is 0.0015000 seconds.
和楼主差别较大,后来我将程序段的顺序变了下,for最先执行,sum最后,变化比较大
s =
5.0000e+011
use for
Elapsed time is 1.969000 seconds.
s =
5.0000e+011
use while
Elapsed time is 2.438000 seconds.
s =
5.0000e+011
use sum
Elapsed time is 0.046000 seconds.
不过当三个程序段放到三个m文件里分别执行的时候,还是
use for
Elapsed time is 0.015000 seconds.
use while
Elapsed time is 0.016000 seconds.
use sum
Elapsed time is 0.047000 seconds.
不知道楼主的for和while用的什么程序段
OldHandle = findobj( \'Name\', \'悲伤\' ) ;if ~isempty(OldHandle),delete(OldHandle) ;end
for Time = \'现在\':\'每一天\':\'永远\',set( gco, \'心情\', \'快乐\');end % 这段代码为你天天快乐而存在