EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
一、 遵守PeRFormance Acceleration的规则 关于什么是“Performance Acceleration”请参阅matlab的帮助文件。我只简要的将
7 K! U) `- e% j2 J/ }: F3 q其规则总结如下7条:7 |' _0 U1 U" M
1、只有使用以下数据类型,matlab才会对其加速:
1 q5 l% R- u$ O( L! [- Ylogical,char,int8,uint8,int16,uint16,int32,uint32,double
) l) K* o& Z% d而语句中如果使用了非以上的数据类型则不会加速,如:numeric,cell,structu
/ Y' u* ^" w$ c6 ]re,single, function handle,java classes,user classes,int64,uint64
( S3 Q" E2 [' i, I0 {* H5 F* N2、matlab不会对超过三维的数组进行加速。
; i( |1 i M5 u0 ?6 l( E3、当使用for循环时,只有遵守以下规则才会被加速:a、for循环的范围只用标量值! T" A1 H- a: t- f7 I" B
来表示;# L. v( D# m) D: D$ s
b、for循环内部的每一条语句都要满足上面的两条规则,即只使用支持加速的数
5 L8 f4 E2 a9 S9 s2 z" c) ]据类型,只使用
% `; ]/ Z, C' ^+ W+ p三维以下的数组;c、循环内只调用了内建函数(build-in function)。' J p0 [+ Y0 r/ @
4、当使用if、elseif、while和switch时,其条件测试语句中只使用了标量值时,将
# D$ m) I* K) S加速运行。: _" I8 H) }1 X) \4 t- H
5、不要在一行中写入多条操作,这样会减慢运行速度。即不要有这样的语句:" _& {- Q& m! {( n* _. c
x = a.name; for k=1:10000, sin(A(k)), end;
) }9 C# b' K. a' J0 w6、当某条操作改变了原来变量的数据类型或形状(大小,维数)时将会减慢运行速, ]6 N' x% `0 d- @/ j2 N! y
度。
9 T3 ^% V1 @1 h7、应该这样使用复常量x = 7 + 2i,而不应该这样使用:x = 7 + 2*i,后者会降低, E* L. `4 ]/ b }2 l
运行速度。 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
& q2 A. w/ g7 T ~& v% x. G6 G%%%%%%%%%%%%%%%%%%%%%%%
) Z6 G1 a8 }1 D二、 遵守三条规则 1、尽量避免使用循环,MATLAB的文档中写到“MATLAB is a matrix language, whic6 x) {$ E; H( z$ U. s
h means it is designed for vector and matrix operations. You can often speed up your M-file c
9 c& L F. W, R1 G a7 r, j code by using& `* E5 S: H2 ?1 F1 J
vectorizing algorithms that take advantage of this design. Vectorizati2 B+ t0 e" Z V3 ^' `0 A* b/ }2 @
on means converting
5 h+ X. H- J1 i# b4 Y& D5 Hfor and while loops to equivalent vector or matrix operations.”。改进
: b. d' ^% y B. f% E! e* [这样的状况有两种方法: a、尽量用向量化的运算来代替循环操作。如将下面的程序: i=0;
- {5 e! B% T# A/ a' ^8 l2 q0 Nfor t = 0:.01:10
4 c; \9 s4 t% z6 x) o0 p3 d6 D- ei = i+1;
% m, t" r; A1 ]1 Q: h2 Xy(i) = sin(t);
8 |* H# X% l" L& Uend0 f& t! E' B; t6 @
替换为:
2 ?: P# t+ F1 T, Y, e& mt = 0:.01:10;# h" d7 m+ T4 E! Q: a/ K, A4 ]1 ^
y = sin(t);
$ ?6 h7 {( J ?! u( z3 m/ F速度将会大大加快。最常用的使用vectorizing技术的函数有:All、diff、i8 v1 ]$ g1 H( M& b( o X
permute、permute、9 c# f9 I5 `; o3 ^2 D% u0 e
reshape、squeeze、any、find、logical、prod、shiftdim、sub2ind、cums1 l! R a' g) {& {( P& s
um、ind2sub、
7 D$ z% @4 K, T" A9 L9 V9 Lndgrid、repmat、sort、sum 等。 请注意matlan文档中还有这样一句补充:“Before taking the time to vectorize your code, read the section on Performance Acceleration.
) `' t! w/ N& s; d; g+ hYou may be able to
; r0 s( Y& g0 Q# Hspeed up your program by just as much using the MATLAB JIT Accelera. J+ V' [* O( ~
tor instead of6 }$ i/ @ E2 _& e! t' L; B
vectorizing.”。何去何从,自己把握。 b、在必须使用多重循环时下,如果两个循环执行的次数不同,则在循环的外环执% h8 C5 o' u4 T$ I; D n
行循环次数少的,
, j& o5 M$ n7 l2 I内环执行循环次数多的。这样可以显著提高速度。 2、a、预分配矩阵空间,即事先确定变量的大小,维数。这一类的函数有zeros、on- m: w3 Z. g2 z/ ]% r8 A
es、cell、struct、
1 D+ \' s5 e; M# L" A9 ?- ^7 Mrepmat等。: }1 l# B U* m$ Y8 L: v" I
b、当要预分配一个非double型变量时使用repmat函数以加速,如将以下代码: A = int8(zeros(100));# W& l) |4 e. y& ]
换成:* L( Q! X1 V! p! m0 J
A = repmat(int8(0), 100, 100);. H0 z! u/ |! y* P% T
c、当需要扩充一个变量的大小、维数时使用repmat函数。 3、a、优先使用matlab内建函数,将耗时的循环编写进MEX-File中以获得加速。7 ^1 M! B2 H3 q' Z$ R M
b、使用Functions而不是Scripts 。 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%+ ~# ~) [, C" r2 s9 _4 Q- E; f2 x1 H
%%%%%%%%%%%%%%%%%%%%%%%% V8 U$ L/ G( I
三、 绝招 你也许觉得下面两条是屁话,但有时候它真的是解决问题的最好方法。9 P" r' m9 |& g( c; a5 E
1、改用更有效的算法# A# j: \% A0 [1 X
2、采用Mex技术,或者利用matlab提供的工具将程序转化为C语言、Fortran语言。
/ s; R) _1 E n6 H1 _2 J3、如果循环比较大的话,将循环部分改成dll调用会快50到100倍,大家试试吧
6 q% h8 \+ t) F& T% f |