如何取得CPU的运行速度

时间:2010-01-27 16:32:16  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:ndows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,ComCtrls, StdCtrls, Buttons;typeTFormCPUSpeed = class(TForm)PageControl: TPageControl;BitBtnStart: TBitBtn;BitBtnStop: TBitBtn;TabSheet: TTabSheet;LabelCPUSpeed: TLabel;LabelInfo: TLabel;LabelWeb: TLabel;procedure Bit...
  正文:参见下面的代码:


unit UCPUSpd;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls, Buttons;

type
TFormCPUSpeed = class(TForm)
PageControl: TPageControl;
BitBtnStart: TBitBtn;
BitBtnStop: TBitBtn;
TabSheet: TTabSheet;
LabelCPUSpeed: TLabel;
LabelInfo: TLabel;
LabelWeb: TLabel;
procedure BitBtnStartClick(Sender: TObject);
procedure BitBtnStopClick(Sender: TObject);
private
{ Private declarations }
Stop: Boolean;
public
{ Public declarations }
end;

var
FormCPUSpeed: TFormCPUSpeed;

implementation

{$R *.DFM}

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 TFormCPUSpeed.BitBtnStartClick(Sender: TObject);
begin
  BitBtnStart.Enabled := False;
  BitBtnStop.Enabled := True;
  
  Stop := False;
  while not Stop do
  begin
    LabelCPUSpeed.Caption := Format('CPU speed: %f MHz', [GetCPUSpeed]);
    Application.ProcessMessages;
  end;
  
  BitBtnStart.Enabled := True;
  BitBtnStop.Enabled := False;
end;

procedure TFormCPUSpeed.BitBtnStopClick(Sender: TObject);
begin
  Stop := True;
end;

end.

"如何取得CPU的运行速度"由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Language/Delphi/2010-01-27/12523.html


关键字:

关于《如何取得CPU的运行速度》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td