ucpuspd.pas
来自「精彩编程百例51~75 其中有 cpu速度测试 检测声卡 查询内存信息 图像处理」· PAS 代码 · 共 99 行
PAS
99 行
unit UCPUSpd;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls, Buttons;
type
TFormCPUSpeed = class(TForm)
BitBtnStart: TBitBtn;
BitBtnStop: TBitBtn;
Label1: TLabel;
Edit1: TEdit;
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;
//定义延时时间
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);
//等待0.01秒
asm
dw 310Fh
mov TimerLo, eax
mov TimerHi, edx
end;
Sleep(DelayTime);
asm //测试cpu运算过程
dw 310Fh
sub eax, TimerLo
sbb edx, TimerHi
mov TimerLo, eax
mov TimerHi, edx
end;
SetThreadPriority(GetCurrentThread, Priority);
//恢复当前线程为初始状态
SetPriorityClass(GetCurrentProcess, PriorityClass);
//恢复当前进程为初始状态
Result := TimerLo / (1000.0 * DelayTime);
//根据测试结果,运算得出cpu主频
end;
procedure TFormCPUSpeed.BitBtnStartClick(Sender: TObject);
begin
BitBtnStart.Enabled := False;
BitBtnStop.Enabled := True;
Stop := False;
while not Stop do
begin
Edit1.text := Format('CPU speed: %f MHz', [GetCPUSpeed]);
//显示测试结果
Application.ProcessMessages;
end;
BitBtnStart.Enabled := True;
BitBtnStop.Enabled := False;
end;
procedure TFormCPUSpeed.BitBtnStopClick(Sender: TObject);
//中止测试
begin
showmessage('8_×,谢谢使用');
Stop := True;
edit1.text:='';
end;
end.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?