Page 1 of 1

Get CPU's speed

Posted: Sat Mar 12, 2005 5:30 pm
by Froggerprogger
Code updated for 5.20+

Hi @ll.
Here's "just another one" to calculate the processor's speed:

[update]using realtime priority now[/update]

Code: Select all

; ghz.f = GetProcessorGHz(waitMs.l)
; returns to get the processors speed and uses waitMs Milliseconds
; for the calculation. (values >= 500 should give an accurate result)
; by Froggerprogger 12.03.05

Procedure.f GetProcessorGHz(waitMs.l)
  Protected Hi.l, Lo.l

  SetPriorityClass_(GetCurrentProcess_(),#REALTIME_PRIORITY_CLASS)  ; switching to realtime priority
  Sleep_(0)                     ; wait for new time-slice

  !RDTSC                        ; load the proc's timestamp to eax & edx
  !MOV [p.v_Lo], eax             ; store eax to Lo
  !MOV [p.v_Hi], edx             ; store edx to Hi

  Sleep_(waitMs)                ; wait for waitMs ms

  !RDTSC                        ; load the proc's timestamp to eax & edx
  !SUB eax, [p.v_Lo]             ; subtract Lo from eax
  !SBB edx, [p.v_Hi]             ; subtract Hi from edx incl. carrybit

  !MOV ecx, dword 1000          ; store 1000 to ecx
  !DIV ecx                      ; divide edx & eax by ecx and store result in eax

  !MOV [p.v_Lo], eax             ; copy result to Lo

  SetPriorityClass_(GetCurrentProcess_(),#NORMAL_PRIORITY_CLASS) ; switching back to normal priority

  ProcedureReturn Lo / (1000.0 * waitMs)
EndProcedure

MessageRequester("","Processorspeed is " + StrF(GetProcessorGHz(1000), 4) + " GHz")

Posted: Wed Mar 16, 2005 1:07 am
by Kale
Nice! works well here :)