CPU SPEED

Just starting out? Need help? Post your questions and find answers here.
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

CPU SPEED

Post by NoahPhense »

I know this topic has been hit A LOT of times. But I need to address it
again. Here is some code: (questions below)

Code: Select all

Procedure.l GetCpuMhz() 
  Global int64val.LARGE_INTEGER 
  !FINIT 
  !rdtsc 
  !MOV dword [v_int64val+4],Edx 
  !MOV dword [v_int64val],Eax 
  !FILD qword [v_int64val] 
  Delay(1000) 
  !rdtsc 
  !MOV dword [v_int64val+4],Edx 
  !MOV dword [v_int64val],Eax 
  !FILD qword [v_int64val] 
  !FSUBR st1,st0 
  int64val\highpart=0 
  int64val\lowpart=1000000 
  !FILD qword [v_int64val] 
  !FDIVR st0,st2 
  !fistp qword [v_int64val] 
  ProcedureReturn int64val\lowpart 
EndProcedure 

; MessageBox_(0, Str(GetCpuMhz()*2), "CPU Speed", #MB_ICONINFORMATION)

Debug GetCpuMhz()*2
Delay(1000)
Debug GetCpuMhz()*2
Delay(1000)
Debug GetCpuMhz()*2
Delay(1000)
Debug GetCpuMhz()*2

End
I don't get steady results on every run. Is there something better?
Are there different formulas out there for calculations?

Like:
- grabbing cpu speed from bios
- grebbing cpu speed from windows
- realtime calculation

- np
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

That's interesting. But it doesn't help with laptops that control processor
speeds.

I need an accurate realtime processor speed.

- np
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Getting the CPU speed from the registry is safest. Code similar to the proc you posted won't give reliable results on multicore machines, but if you look at the registry key

HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\SYSTEM\CentralProcessor\0

you will get good information. Then you can check

HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\SYSTEM\CentralProcessor\1

to see if there's another core.
BERESHEIT
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

First of all, you probably don't. Second, if you use the processor the speed may change.

This gives the correct result here, but it's not perfectly accurate.

Code: Select all

Procedure.q GetCycleCount()
  !rdtsc
  ProcedureReturn 
EndProcedure

Procedure CpuSpeed()
  Protected A.q
  Protected B.q
  A = GetCycleCount()
  Sleep_(100)
  B = GetCycleCount()
  ProcedureReturn (B-A) / 100000
EndProcedure

Debug CpuSpeed()
Obviously the longer you wait, the more accurate.
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

@NoahPhense

Did you review topic
http://www.purebasic.fr/english/viewtopic.php?t=3811 ?

I hadn't looked at this since 2003, but the routine I use is based on the topic above and seems to be very consistent. (Jack's version)

For example, 20 sequential checks on this machine all indicate 1784 Mhz.

The registry reports 1785 Mhz, and the My Computer properties indicate 1.79 Ghz.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

This should be as accurate as any:

Code: Select all

Procedure GetCPUSpeed()
  QueryPerformanceFrequency_(@ulFreq.q) 
  ProcedureReturn ulFreq/1000000
EndProcedure

Debug GetCPUSpeed()
BERESHEIT
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

Netmaestro's code does not work on my 300MHz PC as it returns a value of 1.

Here's another routine that does work for me:

Code: Select all

Procedure.l CpuSpeed()
; Returns CPU speed in MHz
Protected A.q, B.q
!RDTSC
!MOV dword [p.v_A+4],Edx ; A hi
!MOV dword [p.v_A],Eax ; A lo
Sleep_(100)
!RDTSC
!MOV dword [p.v_B+4],Edx ; B hi
!MOV dword [p.v_B],Eax ; B lo
ProcedureReturn (B-A)/100000
EndProcedure
Anthony Jordan
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Some older processors don't support QueryPerformanceFrequency, for those it won't work, true enough.
BERESHEIT
User avatar
Azul
Enthusiast
Enthusiast
Posts: 109
Joined: Fri Dec 29, 2006 9:50 pm
Location: Finland

Post by Azul »

netmaestro wrote:Some older processors don't support QueryPerformanceFrequency, for those it won't work, true enough.
How new it should be?

I got '3' with AMD Athlon(tm) XP 2400+ (2GHz)

update: WinXP SP2
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Not sure about specific hardware, which configurations support high-resolution frequency counters and which don't, but when you do:

Code: Select all

Result = QueryPerformanceFrequency_(@ulFreq.q)
if you find a non-zero value in Result, the high-resolution frequency counter is supported by your hardware, if it's zero your hardware doesn't support it. I'm not too strong in hardware, so the support for this could be coming from the motherboard / bios rather than the processor, I dunno. If somebody knows, a post would be appreciated.
BERESHEIT
CadeX
Enthusiast
Enthusiast
Posts: 124
Joined: Mon Oct 02, 2006 2:56 pm
Location: Australia
Contact:

Post by CadeX »

netmaestro wrote:Some older processors don't support QueryPerformanceFrequency, for those it won't work, true enough.
My processor is quite insulted by that, seeing as its an AMD 64bx2 2.2GHZ Core processor. Amd Hammer Family, That is not old.

I also get a "3" returned.

I did notice that everyone with issues of that code has an AMD.
Pro-Gamer, Programmer, Pro-Grammer
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

I got 1862 with my dual core core2 which runs at 1.86 per core, but as was mentioned, it's not an AMD.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

netmaestro wrote:Some older processors don't support QueryPerformanceFrequency, for those it won't work, true enough.
i have AMD AthlonXP 2800+ (2.08 Ghz) and QueryPerformanceFrequency returns here 3.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Of those posted, Trond's give 1799 on my amd athlon 1.8 and NoahPhense
3600 or 3599.

Neither of the others seem to work properly. netmaestro returns the expected amd 3 and I get an assembler undefined symbol on akj's.

No complaints. just reporting my experience in the interest of science :)

cheers
Post Reply