全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:5924
推到 Plurk!
推到 Facebook!

如何撰寫程式來測試自己程式的效能

 
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-05-09 14:48:13 IP:61.231.xxx.xxx 未訂閱
看到  ■【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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-05-09 14:56:15 IP:61.231.xxx.xxx 未訂閱
沒有可以下載的東西(連個 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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-05-10 09:25:47 IP:61.224.xxx.xxx 未訂閱
除了測量時間的 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
系統時間:2024-04-26 6:06:11
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!