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;
*************
在VC中用以下代码可以测出CPU的主频:
static int time[2]
int cpuclock;
在InitInstance中
SetTimer(hWnd,1,1000,NULL)
在消息处理过程中
case WM_CREATE;
_asm{
rdtsc //一个64bit的时间标记计数器
mov ecx,offset time
mov [ecx+0],edx
mov [ecx+4],eax
}
break;
case WM_TIMER:
_asm
{
rdtsc
mov ebx,offset time
sub eax,[ebx+4]
sbb edx,[ebx+0]
mov ecx,1000000
div ecx
mov cpuclock,eax
}
但不知在DELPHI中如何编程测试?还有CPU的类型等等!
--------------------------------------------------------------------------------
来自:cat.yy 时间:00-12-29 16:17:03 ID:427844
SetTimer(handle,1,1000,NULL);
...
...(var msg: message)...
if msg.message=WM_CREATE then
begin
asm
rdtsc //一个64bit的时间标记计数器
mov ecx,offset time
mov [ecx+0],edx
mov [ecx+4],eax
end;
end;
if msg.message=WM_TIMER then
begin
asm
rdtsc
mov ebx,offset time
sub eax,[ebx+4]
sbb edx,[ebx+0]
mov ecx,1000000
div ecx
mov cpuclock,eax
end;
end;
*************
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;