风花雪月 发表于 2005-8-8 09:50

加速matlab运行的三重境界

加速matlab运行的三重境界<BR>发信站: BBS 哈工大紫丁香站 <BR><BR>加速matlab运行的三重境界<BR><BR>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<BR>%%%%%%%%%%%%%%%%%%%%%%%<BR>一、 遵守Performance Acceleration的规则<BR>二、 遵守三条规则<BR>三、 绝招<BR><BR>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<BR>%%%%%%%%%%%%%%%%%%%%%%%<BR>一、 遵守Performance Acceleration的规则<BR><BR>关于什么是“Performance Acceleration”请参阅matlab的帮助文件。我只简要的将<BR>其规则总结如下7条:<BR>1、只有使用以下数据类型,matlab才会对其加速:<BR>logical,char,int8,uint8,int16,uint16,int32,uint32,double<BR>而语句中如果使用了非以上的数据类型则不会加速,如:numeric,cell,structu<BR>re,single,<BR><BR>function handle,java classes,user classes,int64,uint64<BR>2、matlab不会对超过三维的数组进行加速。<BR>3、当使用for循环时,只有遵守以下规则才会被加速:a、for循环的范围只用标量值<BR>来表示;<BR>b、for循环内部的每一条语句都要满足上面的两条规则,即只使用支持加速的数<BR>据类型,只使用<BR>三维以下的数组;c、循环内只调用了内建函数(build-in function)。<BR>4、当使用if、elseif、while和switch时,其条件测试语句中只使用了标量值时,将<BR>加速运行。<BR>5、不要在一行中写入多条操作,这样会减慢运行速度。即不要有这样的语句:<BR>x = a.name; for k=1:10000, sin(A(k)), end;<BR>6、当某条操作改变了原来变量的数据类型或形状(大小,维数)时将会减慢运行速<BR>度。<BR>7、应该这样使用复常量x = 7 + 2i,而不应该这样使用:x = 7 + 2*i,后者会降低<BR>运行速度。<BR><BR>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<BR>%%%%%%%%%%%%%%%%%%%%%%%<BR>

风花雪月 发表于 2005-8-8 09:50

回复:(风花雪月)加速matlab运行的三重境界

二、 遵守三条规则<BR><BR>1、尽量避免使用循环,MATLAB的文档中写到“MATLAB is a matrix language, whic<BR>h means it is designed<BR><BR>for vector and matrix operations. You can often speed up your M-file c<BR>ode by using<BR>vectorizing algorithms that take advantage of this design. Vectorizati<BR>on means converting<BR>for and while loops to equivalent vector or matrix operations.”。改进<BR>这样的状况有两种方法:<BR><BR>a、尽量用向量化的运算来代替循环操作。如将下面的程序:<BR><BR>i=0;<BR>for t = 0:.01:10<BR>i = i+1;<BR>y(i) = sin(t);<BR>end<BR>替换为:<BR>t = 0:.01:10;<BR>y = sin(t);<BR>速度将会大大加快。最常用的使用vectorizing技术的函数有:All、diff、i<BR>permute、permute、<BR>reshape、squeeze、any、find、logical、prod、shiftdim、sub2ind、cums<BR>um、ind2sub、<BR>ndgrid、repmat、sort、sum 等。<BR><BR>请注意matlan文档中还有这样一句补充:“Before taking the time to<BR><BR>vectorize your code, read the section on Performance Acceleration.<BR>You may be able to<BR>speed up your program by just as much using the MATLAB JIT Accelera<BR>tor instead of<BR>vectorizing.”。何去何从,自己把握。<BR><BR>b、在必须使用多重循环时下,如果两个循环执行的次数不同,则在循环的外环执<BR>行循环次数少的,<BR>内环执行循环次数多的。这样可以显著提高速度。<BR><BR>2、a、预分配矩阵空间,即事先确定变量的大小,维数。这一类的函数有zeros、on<BR>es、cell、struct、<BR>repmat等。<BR>b、当要预分配一个非double型变量时使用repmat函数以加速,如将以下代码:<BR><BR>A = int8(zeros(100));<BR>换成:<BR>A = repmat(int8(0), 100, 100);<BR>c、当需要扩充一个变量的大小、维数时使用repmat函数。<BR><BR>3、a、优先使用matlab内建函数,将耗时的循环编写进MEX-File中以获得加速。<BR>b、使用Functions而不是Scripts 。<BR><BR>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<BR>%%%%%%%%%%%%%%%%%%%%%%%<BR>三、 绝招<BR><BR>你也许觉得下面两条是屁话,但有时候它真的是解决问题的最好方法。<BR>1、改用更有效的算法<BR>2、采用Mex技术,或者利用matlab提供的工具将程序转化为C语言、Fortran语言。<BR><BR>关于如何将M文件转化为C语言程序运行,可以参阅本版帖子:“总结:m文件转化为c/c++<BR>语言文件,VC编译”。 <BR>

suffer 发表于 2005-8-9 21:03

<P>怎么加速都不如fortran.</P><br><br>个人认为计算量小的用matlab不错,计算量大的还是别考虑了,直接fortran吧,C++也行,呵呵,所以加不加速无所谓了
[此贴子已经被作者于2005-8-9 21:05:07编辑过]
页: [1]
查看完整版本: 加速matlab运行的三重境界