如何撰寫程式來測試自己程式的效能 |
|
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
看到
■【Delphi】【分享】ProDelphi--協助Delphi 性能調試
http://delphi.ktop.com.tw/topic.php?TOPIC_ID=22780 ■【Delphi】【發表】如何用 ProDelphi 測試你的程式效能
http://delphi.ktop.com.tw/topic.php?TOPIC_ID=30145 對於 BCB 的使用者... 真的只能乾瞪眼 不過... >> 立刻用 > ) 利用上例相同的方式,可以在你需要量測的前後加上類似的 > <>沒空更新的網頁...
href="http://dllee.adsldns.org">http://dllee.adsldns.org 介紹Shells,LiteStep,GeoShell.... 發表人 - dllee 於 2003/05/09 15:48:37
------
http://www.ViewMove.com |
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
沒有可以下載的東西(連個 code 都沒有 )放在作品區好像有點怪...
以下是上篇提到的 取得 class="code">
double __fastcall GetCPUSpeed(void)
{
#define _MeasureTime_ 500 // measure time in ms
DWORD TimerHi,TimerLo;
int PriorityClass,Priority;
PriorityClass = GetPriorityClass(GetCurrentProcess());
Priority = GetThreadPriority(GetCurrentThread());
SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
Sleep(10);
asm{
dw 0x310F
mov TimerLo, eax
mov TimerHi, edx
};
Sleep(_MeasureTime_);
asm{
dw 0x310F
sub eax, TimerLo
sbb edx, TimerHi
mov TimerLo, eax
mov TimerHi, edx
};
SetThreadPriority(GetCurrentThread(), Priority);
SetPriorityClass(GetCurrentProcess(), PriorityClass);
return TimerLo / (1000.0 * _MeasureTime_);
}
將 Sleep(_MeasureTime_); 的部分改成你要測的 code,由 TimerHi 及 TimerLo 組合的 64 bits 就是 code 執行的時脈數。 沒空更新的網頁...
http://dllee.ktop.com.tw C及指標教學,計算機概論,資訊管理導論... http://dllee.adsldns.org 介紹Shells,LiteStep,GeoShell....
------
http://www.ViewMove.com |
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
除了測量時間的 code 外,還要有不影響原程式碼的功效,再不測試程式效能時,可以與沒有加入測試 code 時一樣快!
要作到這一點,只需善用 #ifdef/#else/#endif 例如: 原始碼:
void MySourceCode(void) { int a,b; a=DoSomeThingA(); b=DoSomeThingB(); for(int i=0;i<100;i ) a=DoSomeThingAB(a,b); }加上測試效能的 code #ifdef MEASURE_PERFORMANCE int MySoueceCode_ExeCount; __int64 MySourceCode_TotalTime; #endif void MySourceCode(void) { #ifdef MEASURE_PERFORMANCE DWORD TimerHi,TimerLo; asm{ dw 0x310F mov TimerLo, eax mov TimerHi, edx }; #endif int a,b; a=DoSomeThingA(); b=DoSomeThingB(); for(int i=0;i<100;i ) a=DoSomeThingAB(a,b); #ifdef MEASURE_PERFORMANCE asm{ dw 0x310F sub eax, TimerLo sbb edx, TimerHi mov TimerLo, eax mov TimerHi, edx }; MySoueceCode_ExeCount ; MySourceCode_TotalTime = TimerLo (__int64(TimerHi)<<32); #endif }如此,當你需要測試效能時,在檔頭加上 #define MEASURE_PERFORMANCE 或如 ProDelphi 的說明把它寫在 Project Options 內都行,再 Rebuild All... 當然,以上只是一個小範例,還需要其他的 code 將 MySoueceCode_ExeCount 及 MySourceCode_TotalTime 顯示或 Log 成檔案,最後還要求得每次平均 TimeStampCounter 數及換算成真實的秒數。 而當您不需要再測試時,只需將 // #define MEASURE_PERFORMANCE Remark 起來,或是從 Project Options 中將它去除,再 Rebuild 一次,則與原 code 就是一樣的了。 沒空更新的網頁... http://dllee.ktop.com.tw C及指標教學,計算機概論,資訊管理導論... http://dllee.adsldns.org 介紹Shells,LiteStep,GeoShell....
------
http://www.ViewMove.com |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |