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;

解决方案 »

  1.   

    一个函数:function TForm1.GetCpuSpeed: Extended;
    var
        t, mhi, mlo, nhi, nlo: dword;
        shr32 : comp;
    begin
        shr32 := 65536;
        shr32 := shr32 * 65536;
        t := GetTickCount;
        while t = GetTickCount do ;
        asm
            DB 0FH,031H // rdtsc
            mov mhi,edx
            mov mlo,eax
        end;
        while GetTickCount < (t + 1000) do ;
        asm
            DB 0FH,031H // rdtsc
            mov nhi,edx
            mov nlo,eax
        end;
        Result := ((nhi * shr32 + nlo) - (mhi * shr32 + mlo)) / 1E6;
    end;
      

  2.   

    一个函数:function TForm1.GetCpuSpeed: Extended;
    var
        t, mhi, mlo, nhi, nlo: dword;
        shr32 : comp;
    begin
        shr32 := 65536;
        shr32 := shr32 * 65536;
        t := GetTickCount;
        while t = GetTickCount do ;
        asm
            DB 0FH,031H // rdtsc
            mov mhi,edx
            mov mlo,eax
        end;
        while GetTickCount < (t + 1000) do ;
        asm
            DB 0FH,031H // rdtsc
            mov nhi,edx
            mov nlo,eax
        end;
        Result := ((nhi * shr32 + nlo) - (mhi * shr32 + mlo)) / 1E6;
    end;
      

  3.   

    function GetCPUSpeed: Double;
    const
      DelayTime = 500; // measure time in ms
    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;procedure TForm1.BitBtn1Click(Sender: TObject);
    begin
      CpuSpeedEdit.Text:=FloatToStr(GetCPUSpeed);
    end;
      

  4.   

    给你一个函数
    function GetCPUSpeed: Double;
    const
      DelayTime = 500;
    var
      TimerHi, TimerLo: DWORD;
      PriorityClass, Priority: Integer;
    begin
    try
      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);
      except
      Result := 0;
      end;
    end;