|
1.首先要學(xué)會用profiler分析代碼效率. 1.1. 打開profiler. 在我的機器上是:在matlab desktop下,Desktop->Profiler. 在M文件編輯器下,Tools->Open Profiler. 1.2. 運行profiler 可以把要運行的code拷入Run this code后面的輸入框里。 You can run this example [t,y] = ode23('lotka',[0 2],[20;20]) 也可以輸入要運行的M文件名。 1.3.Click Start Profiling (or press Enter after entering the statement). 1.4. 查看Profile Detail Report 會告知你哪些代碼消耗了多少時間,可以找到哪些函數(shù)或那些代碼行消耗了主要的時間,或者是經(jīng)常被調(diào)用。 也可以用stopwatch Timer函數(shù),計算程序消耗時間 Use tic and toc as shown here. tic - run the program section to be timed - toc 2、 加速方法1——向量化編程
MATLAB programs are interpretted. This would seem to make it inapproapriate for large scale scientific computing. The power of MATLAB is realized with its extensive set of libraries which are compiled or are carefully coded in MATLAB to utilize "vectorization". The concept of vectorization is central to understanding how to write efficient MATLAB code. Vectorized code takes advantage, wherever possible, of operations involving data stored as vectors. This even applies to matrices since a MATLAB matrix is stored (by columns) in contiguous locations in the computer's RAM. The speed of a numerical algorithm in MATLAB is very sensitive to whether or not vectorized operations are used. MATLAB is a matrix language, which means it is designed for vector and matrix operations. You can often speed up your M-file code by using vectorizing algorithms that take advantage of this design. Vectorization means converting for and while loops to equivalent vector or matrix operations. i = 0; for t = 0:.01:1000; i = i+1; y(i) = sin(t); end 運行時間為30.776秒。 這段代碼是很自然的從C語言的形式轉(zhuǎn)化而來的,但其效率很低!Matlab是實時為變量分配內(nèi)存的,在第一遍循環(huán)時(即i=1時),Matlab為y向量(長度為1)分配內(nèi)存。以后每執(zhí)行一次循環(huán),Matlab都會在y的末尾附加新的元素。這不僅導(dǎo)致分配內(nèi)存的調(diào)用的增加,也使得y的各個元素在內(nèi)存中的分布不是連續(xù)的(就像數(shù)據(jù)結(jié)構(gòu)中數(shù)組和鏈表的區(qū)別)。相比之下,下面的代碼效率提高不少: 改為向量化代碼: t = 0:.01:1000; y = sin(t); 運行時間為0秒。 第一個語句分配了一個連續(xù)的內(nèi)存空間來存儲具有多個元素的向量t。類似的,第二個語句在分配內(nèi)存時,也是分配了一個連續(xù)的內(nèi)存空間來存儲具有多個元素的向量y。撇去計算sin的消耗不算,就內(nèi)存分配命令的執(zhí)行次數(shù)和對向量元素訪問的方便程度來說,高下立見。 Functions Used in Vectorizing,Some of the most commonly used functions for vectorizing are:all,diff,ipermute,permute, reshape,squeeze,any,find,logical,prod,shiftdim,sub2ind,cumsum,ind2sub,ndgrid,repmat, sort,sum
3、 加速方法2——Preallocating Arrays(預(yù)分配空間) You can often improve code execution time by preallocating the arrays that store output results. Preallocation makes it unnecessary for MATLAB to resize an array each time you enlarge it. Use the appropriate preallocation function for the kind of array you are working with. Preallocation also helps reduce memory fragmentation if you work with large matrices. 雖然Matlab會自動調(diào)整變量的大小,我們最好還是預(yù)先為變量分配內(nèi)存空間。因為這樣可以使調(diào)用內(nèi)存分配命令的次數(shù)降為1,也可以使變量在內(nèi)存中連續(xù)存儲(當(dāng)變量為矩陣時是按列在內(nèi)存中連續(xù)存儲)。而所謂“預(yù)先為變量分配內(nèi)存空間” ,是指在知道變量的大小的情況下,在變量中的任何一個元素都未被引用之前,創(chuàng)建一個大小和其一致的變量。 例如,下面的代碼自上而下,運行效率是不斷提高的: 1 dx = pi/30; 2 nx = 1 + 2*pi/dx; 3 nx2 = nx/2; 4 5 for i = 1:nx2 6 x(i) = (i-1)*dx; 7 y(i) = sin(3*x(i)); 8 end 9 10 for i = nx2+1:nx 11 x(i) = (i-1)*dx; 12 y(i) = sin(5*x(i)); 13 end
1 dx = pi/30; 2 nx = 1 + 2*pi/dx; 3 nx2 = nx/2; 4 5 x = zeros(1,nx); % 為向量x預(yù)分配內(nèi)存 6 y = zeros(1,nx); % 為向量y預(yù)分配內(nèi)存 7 8 for i = 1:nx2 9 x(i) = (i-1)*dx; 10 y(i) = sin(3*x(i)); 11 end 12 13 for i = nx2+1:nx 14 x(i) = (i-1)*dx; 15 y(i) = sin(5*x(i)); 16 end
1 x = 0:pi/30:2*pi; % 計算向量x的值 2 nx = length(x); 3 nx2 = nx/2; 4 5 y = x; % 為向量y預(yù)分配內(nèi)存 6 7 for i = 1:nx2 8 y(i) = sin(3*x(i)); 9 end 10 11 for i = nx2+1:nx 12 y(i) = sin(5*x(i)); 13 end
1 x = 0:pi/30:2*pi; % 計算向量x的值 2 nx = length(x); 3 nx2 = nx/2; 4 5 y = x; % 為向量y預(yù)分配內(nèi)存 6 7 y(1:nx2) = sin(3*x(1:nx2)); % 計算y的第1部分的值 8 y(nx2+1:nx) = sin(5*x(nx2+1:nx)); % 計算y的第2部分的值
4、加速其他方法: Coding Loops in a MEX-File for Speed If there are instances where you must use a for loop, consider coding the loop in a MEX-file. In this way, the loop executes much more quickly since the instructions in the loop do not have to be interpreted each time they execute. Functions Are Faster Than Scripts Your code will execute more quickly if it is implemented in a function rather than a script. Every time a script is used in MATLAB, it is loaded into memory and uated one line at a time. Functions, on the other hand, are compiled into pseudo-code and loaded into memory once. Therefore, additional calls to the function are faster. Load and Save Are Faster Than File I/O Functions If you have a choice of whether to use load and save instead of the MATLAB file I/O routines, choose the former. The load and save functions are optimized to run faster and reduce memory fragmentation. Avoid Large Background Processes Avoid running large processes in the background at the same time you are executing your program in MATLAB. This frees more CPU time for your MATLAB session. 5. 多線程 在matlab desktop里,F(xiàn)ile->Preferences->General->Multithreading, 看是否選擇了Enable Multithreaded Computation。 如果沒選,check it, 看是否有提速。 |
|
|