Get CPU's speed

Share your advanced PureBasic knowledge/code with the community.
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Get CPU's speed

Post 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")
%1>>1+1*1/1-1!1|1&1<<$1=1
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Nice! works well here :)
--Kale

Image
Post Reply