2013年6月10日 星期一

matlab 計算程式運算時間

matlab中有兩個計算程式CPU的耗費時間方法

第一個為最常用的tic,toc。
tic
運算程式
toc
實際例子 Example                                                                                
  tic                                  
n=1000;                                                          
A=rand(n,n);                                                          
b=rand(n,1);  
x = A\b;                                                                             
   toc                                                                    
結果 Result
Elapsed time is 0.069797 seconds.

第二個為 profile,對於整個執行過程的函數做個別計算CPU的耗費時間
實際例子 Example                                                                                
  profile on    %執行函數運算時間計算                                        
x1 = randn(1,100);                                                          
x2 = randn(1,100);                                                          
x3 = randn(1,100);  
HistValuex1 = hist(x1);                                                                              
HistValuex2 = hist(x2);                                                                   
HistValuex3 = hist(x3);                                                                   
mean(x1)                                                                                          
var(x1) 
profile viewer     %顯示計算時間
p = profile('info');%定義計算時間所暫存的記憶體位置  
profsave(p,'profile_results')   %將計算時間存檔
結果 Result

沒有留言: