myamanda

博客园 首页 新随笔 联系 订阅 管理
function CPUSpeed: Double; 
const 
  DelayTime = 500; // 时间单位是毫秒 
var 
  TimerHi, TimerLo: DWORD; 
  PriorityClass, Priority: Integer; 
begin 
     PriorityClass := GetPriorityClass(GetCurrentProcess); 
     Priority := GetThreadPriority(GetCurrentThread); 
     SetPriorityClass(GetCurrentProcess, REALTIME_PRIORITY_CLASS); 
     SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_TIME_CRITICAL); 

     Sleep(10); 

     asm 
        dw 310Fh // rdtsc 
        mov TimerLo, eax 
        mov TimerHi, edx 
     end; 

     Sleep(DelayTime); 

     asm 
        dw 310Fh // rdtsc 
        sub eax, TimerLo 
        sbb edx, TimerHi 
        mov TimerLo, eax 
        mov TimerHi, edx 
     end; 

     SetThreadPriority(GetCurrentThread, Priority); 
     SetPriorityClass(GetCurrentProcess, PriorityClass); 

     Result := TimerLo / (1000.0 * DelayTime); 
end;
posted on 2009-12-23 15:04  myamanda  阅读(340)  评论(0编辑  收藏  举报