Presentation is loading. Please wait.

Presentation is loading. Please wait.

張智星 jang@mirlab.org http://mirlab.org/jang 清大資工系 多媒體檢索實驗室 MATLAB 程式設計進階篇 程式計時 張智星 jang@mirlab.org http://mirlab.org/jang 清大資工系 多媒體檢索實驗室.

Similar presentations


Presentation on theme: "張智星 jang@mirlab.org http://mirlab.org/jang 清大資工系 多媒體檢索實驗室 MATLAB 程式設計進階篇 程式計時 張智星 jang@mirlab.org http://mirlab.org/jang 清大資工系 多媒體檢索實驗室."— Presentation transcript:

1 張智星 jang@mirlab.org http://mirlab.org/jang 清大資工系 多媒體檢索實驗室
MATLAB 程式設計進階篇 程式計時 張智星 清大資工系 多媒體檢索實驗室

2 1-1 整段程式碼的計時方法 相關指令 本章範例之執行環境 tic & toc clock & etime cputime
1-1 整段程式碼的計時方法 相關指令 tic & toc clock & etime cputime 本章範例之執行環境 OS: Windows 7 CPU: Intel Core i7-2670QM, 2.20GHz RAM: 8GB MATLAB: (R2011a)

3 單一碼表計時:tic & toc 單一碼表計時:
tic 和 toc 指令,是最簡單的程式計時方法,只要將整段程式碼置於這兩個指令之中,MATLAB 就會自動計算程式執行所花費的時間。 提示:tic 及 toc 即是按下碼表的聲音。: 範例1-1:tictoc01.m 結果: Elapsed time is seconds tic % 開始計時 Z=inv(rand(2000)); % inv 指令是用來計算反矩陣 toc % 結束計時

4 多個碼表計時:tic & toc 多個碼表計時: 需指定馬表(或計時器)如下 範例:tictoc02.m timer=tic; …
time=toc(timer); 範例:tictoc02.m timer1=tic; % 馬表一開始計時 n=100*(1:10); for i=1:length(n) timer2=tic; % 馬表二開始計時 z=inv(rand(n(i))); % inv 指令是用來計算反矩陣 time(i)=toc(timer2); % 馬表二停止計時 end fprintf('Overall time = %f sec\n', toc(timer1)); % 馬表一停止計時 plot(n, time, '.-');

5 clock 指令 clock: clock 指令可傳回現在的時間所形成的向量,包含 6 個元素,分別是年、月、日、時、分、秒 例如:
執行: >> round(clock) % 傳回現在的時間,並以整數形式顯示 結果: ans = 代表現在時間是 2011 年 11 月 15 日 23 時 26 分 39 秒

6 etime 指令 etime: etime 指令可傳回兩個時間的差值,並以秒數表示
將 clock 和 etime 指令合併使用,就可以計算一段程式碼的執行時間 範例1-2:etime01.m 結果: elapsedTime = t0 = clock; % 記錄現在的時間 a = inv(rand(2000)); % 執行反矩陣運算 elapsedTime = etime(clock, t0) % 計算所耗費的總時間

7 cputime 指令 cputime: cputime可傳回 MATLAB 從啟動後所占用的 CPU 時間
t0 = cputime; % 記錄現在的時間 a = inv(rand(500)); % 執行反矩陣運算 cpuTime = cputime-t0 % 計算 CPU 所耗費的時間

8 1-1 整段程式碼的計時方法 cputime: cputime 指令回傳的時間並不包含讀檔、關檔等 I/O 運算,所以其值會小於整段程式碼的實際執行時間 下面範例測試 etime 和 cputime 的差異

9 1-1 整段程式碼的計時方法 範例1-4:cputime02.m
1-1 整段程式碼的計時方法 範例1-4:cputime02.m 結果 : elapsedTime = cpuTime = mat = magic(50); t0 = clock; for i = 1:10; mesh(mat); end elapsedTime = etime(clock, t0) % 顯示實際經過時間 t0 = cputime; cpuTime = cputime-t0 % 顯示 CPU 佔用時間

10 1-2 個別函數的計時方法 程式計時器: 計時器可以分別計算每一個指令所佔用的執行時間,並列出詳細的統計表
1-2 個別函數的計時方法 程式計時器: 計時器可以分別計算每一個指令所佔用的執行時間,並列出詳細的統計表 MATLAB 計時器的指令是 profile 範例1-5:profile01.m profile on -detail mmex % 啟動計時器(只對 M/MEX 檔案進行計時) for i = 1:1000 a = inv(rand(100)); % 計算 100x100 亂數矩陣的反矩陣 b = mean(rand(100)); % 計算 100x100 亂數矩陣的每一直行平均值 end profile off % 停止計時器 profile report % 呈現計時結果

11 1-2 個別函數的計時方法 範例1-5:profile01.m

12 1-2 個別函數的計時方法 如果你點選上圖中的「mean」可以開啟下列視窗:

13 1-2 個別函數的計時方法 程式計時器: 欲使 profile 指令也對內建指令進行計時,可在 profile on 之後接上「-detail builtin」 範例1-6:profile02.m profile on -detail builtin % 啟動計時器,對 M/MEX 檔案及內建函式計時 for i = 1:1000 a = inv(rand(100)); % 計算 100x100 亂數矩陣的反矩陣 b = mean(rand(100)); % 計算 100x100 亂數矩陣的每一直行平均值 end profile off % 停止計時器 profile report % 呈現計時結果

14 1-2 個別函數的計時方法 範例1-6:profile02.m

15 1-2 個別函數的計時方法 若點選上圖中的任一個函式連結 ,就可以更清楚地看出每一個指令的 parent 指令(即呼叫此指令的上層指令) 和 children 指令(即被此指令所呼叫的下層指令) ,以及他們各自所佔用的時間

16 1-3 程式計時器的其他功能 profile('status'): profile(‘status’)可傳回計時器的狀態
1-3 程式計時器的其他功能 profile('status'): profile(‘status’)可傳回計時器的狀態 例如: >> status = profile('status') 結果: status = ProfilerStatus: 'off' DetailLevel: 'builtin‘ Timer: 'real' HistoryTracking: 'off'

17 1-3 程式計時器的其他功能 profile('info'): 所有的計時資訊都可由 profile('info') 傳回
1-3 程式計時器的其他功能 profile('info'): 所有的計時資訊都可由 profile('info') 傳回 例如: >> info = profile('info') 結果: info = FunctionTable: [10x1 struct] FunctionHistory: [2x0 double] ClockPrecision: e-008 ClockSpeed: 2500 Name: 'MATLAB'

18 1-3 程式計時器的其他功能 如果我們要使用程式碼來抓取每一個函數的計時結果,就可以使用 profile('info') 所傳回來的物件以及 FunctionTable 的欄位來達成所需的功能

19 1-3 程式計時器的其他功能 取得與 inv 指令相關的計時資訊 例如: >>info.FunctionTable(2) 結果:
1-3 程式計時器的其他功能 取得與 inv 指令相關的計時資訊 例如: >>info.FunctionTable(2) 結果: ans = CompleteName: 'C:\Program Files\MATLAB71\bin\win32\numerics.dll>inv' FunctionName: 'inv' FileName: 'C:\Program Files\MATLAB71\bin\win32\numerics.dll' Type: 'Builtin-function' NumCalls: 1000 TotalTime: TotalRecursiveTime: Children: [0x1 struct] Parents: [0x1 struct] ExecutedLines: [0x4 double] IsRecursive: 0 AcceleratorMessages: {1x0 cell}

20 1-4 本章指令彙整 指令 功能 tic 程式碼 Toc
1-4 本章指令彙整 指令 功能 tic 程式碼 Toc tic 和 toc 指令,是最簡單的程式計時方法,只要整段程式碼置於這兩個指令之中,MATLAB 就會自動計算程式執行所花費的時間 clock 傳回現在的時間所形成的整數向量,包含六個元素,分別是年、月、日、時、分、秒 etime(t1, t2) 傳回兩個時間 t1 及 t2 的差值,並以秒數表示 cputime 傳回 MATLAB 從啟動後所占用的 CPU 時間,不包含讀檔、關檔等 I/O 運算的時間 profile on 清除舊的計時資料,並開始對各個函數(不含內建指令)進行計時 profile on –detail mmex 清除舊的計時資料,並開始對各個函數(含m 指令及mex 指令,此為預設值)進行計時

21 1-4 本章指令彙整 profile on –detail builtin 清除舊的計時資料,並開始對各個函數(含內建指令)進行計時
1-4 本章指令彙整 profile on –detail builtin 清除舊的計時資料,並開始對各個函數(含內建指令)進行計時 profile off 停止計時器 profile resume 啟動計時器,但保留舊的計時資料 profile clear 清除所有的計時資料 profile('reviewer') 或 profile reviewer 停止計時器,並在Profiler視窗呈現計時結果 profile('status') 或 profile status 傳回計時器的狀態 profile('info') 或 profile info 傳回所有的計時資訊 profile('report') 或 profile report


Download ppt "張智星 jang@mirlab.org http://mirlab.org/jang 清大資工系 多媒體檢索實驗室 MATLAB 程式設計進階篇 程式計時 張智星 jang@mirlab.org http://mirlab.org/jang 清大資工系 多媒體檢索實驗室."

Similar presentations


Ads by Google