加速matlab运行的三重境界1 _/ a( U8 P" w$ K; h
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5 m, x& P3 c! b3 \- v/ m
%%%%%%%%%%%%%%%%%%%%%%% m2 P3 K/ r- {
一、 遵守Performance Acceleration的规则, H I: h. r% k9 m/ Y
二、 遵守三条规则
三、 绝招3 t! E& s: c9 `4 u0 R5 q
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%! x% v0 Z$ B& E# F9 m% i3 p
%%%%%%%%%%%%%%%%%%%%%%%
一、 遵守Performance Acceleration的规则4 T( x n# K$ }5 i2 O$ e4 J4 ~
关于什么是“Performance Acceleration”请参阅matlab的帮助文件。我只简要的将
其规则总结如下7条:
1、只有使用以下数据类型,matlab才会对其加速:8 V( j6 p# ]; z. @5 s
logical,char,int8,uint8,int16,uint16,int32,uint32,double
而语句中如果使用了非以上的数据类型则不会加速,如:numeric,cell,structu
re,single,' S5 i0 {" ^4 I/ ~" [- D
function handle,java classes,user classes,int64,uint64
2、matlab不会对超过三维的数组进行加速。
3、当使用for循环时,只有遵守以下规则才会被加速:a、for循环的范围只用标量值) t- S/ V$ R$ m" l4 i. T
来表示;
b、for循环内部的每一条语句都要满足上面的两条规则,即只使用支持加速的数
据类型,只使用# ~) P! d: P2 |( T: R
三维以下的数组;c、循环内只调用了内建函数(build-in function)。
4、当使用if、elseif、while和switch时,其条件测试语句中只使用了标量值时,将
加速运行。0 S7 Z$ D7 W. ]- F6 R
5、不要在一行中写入多条操作,这样会减慢运行速度。即不要有这样的语句:: U# d" o+ Q: d4 [+ F' A
x = a.name; for k=1:10000, sin(A(k)), end;7 Y6 Z# w1 F' U7 w
6、当某条操作改变了原来变量的数据类型或形状(大小,维数)时将会减慢运行速9 F3 L. _' _8 _$ o& f D
度。
7、应该这样使用复常量x = 7 + 2i,而不应该这样使用:x = 7 + 2*i,后者会降低! j' P8 \5 w3 E5 U% E/ r" t
运行速度。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%1 N4 K3 u/ B; t) C! {
%%%%%%%%%%%%%%%%%%%%%%%
二、 遵守三条规则; p4 G* k; i9 M p+ k( u
1、尽量避免使用循环,MATLAB的文档中写到“MATLAB is a matrix language, whic
h means it is designed
for vector and matrix operations. You can often speed up your M-file c( ]1 M( [# ?/ S/ d/ k w
ode by using
vectorizing algorithms that take advantage of this design. Vectorizati
on means converting6 E# h- W* l: S! |1 s' x
for and while loops to equivalent vector or matrix operations.”。改进% I2 |' U( {# U$ U4 c& [8 Q- m
这样的状况有两种方法:
a、尽量用向量化的运算来代替循环操作。如将下面的程序:" }/ u5 T& o$ Z8 d4 M7 b1 w
i=0;
for t = 0:.01:10& o) G% A+ u7 I/ S: R& U; o
i = i+1;
y(i) = sin(t);# l3 v4 q2 P" k% l5 T
end7 Q4 R! ]3 |/ d7 d
替换为:+ Z! i* S' l' W3 a& |( A( w
t = 0:.01:10;
y = sin(t);
速度将会大大加快。最常用的使用vectorizing技术的函数有:All、diff、i: {9 `" i# F# g z3 h5 ~% ~
permute、permute、/ X7 a+ I6 `+ X
reshape、squeeze、any、find、logical、prod、shiftdim、sub2ind、cums
um、ind2sub、0 G: f% i4 N$ c6 j: R
ndgrid、repmat、sort、sum 等。* I" I$ {* M) n0 [ k
请注意matlan文档中还有这样一句补充:“Before taking the time to" k( j6 y5 v7 s+ f
vectorize your code, read the section on Performance Acceleration.
You may be able to
speed up your program by just as much using the MATLAB JIT Accelera! [; s; [0 ]; u) H. f2 ^+ v0 L
tor instead of7 @" g9 G2 y, V
vectorizing.”。何去何从,自己把握。+ O2 A& N* l6 r# q
b、在必须使用多重循环时下,如果两个循环执行的次数不同,则在循环的外环执
行循环次数少的,/ _$ t6 ~6 s8 O) z# w" P% j: u
内环执行循环次数多的。这样可以显著提高速度。
2、a、预分配矩阵空间,即事先确定变量的大小,维数。这一类的函数有zeros、on* T: g1 D( w. C: A
es、cell、struct、1 c3 [: h% x7 t+ b2 R
repmat等。& N" x- M _, P+ i7 }
b、当要预分配一个非double型变量时使用repmat函数以加速,如将以下代码:# j/ U8 o5 y/ M
A = int8(zeros(100));8 X% }: w) b* Y1 B# ^! f
换成:
A = repmat(int8(0), 100, 100);
c、当需要扩充一个变量的大小、维数时使用repmat函数。
3、a、优先使用matlab内建函数,将耗时的循环编写进MEX-File中以获得加速。4 s7 c, t: t4 Z* T* s
b、使用Functions而不是Scripts 。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%* c* {' b. Q. Q) b
%%%%%%%%%%%%%%%%%%%%%%%
三、 绝招) C2 ~7 O9 A9 p8 i- O
你也许觉得下面两条是屁话,但有时候它真的是解决问题的最好方法。
1、改用更有效的算法/ d7 }4 @) b+ Q' Z/ b0 p( w% Q5 o6 s
2、采用Mex技术,或者利用matlab提供的工具将程序转化为C语言、Fortran语言。
| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) | Powered by Discuz! X2.5 |