CPU Speed

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hi-Toro.

Hi all,

This should return the CPU speed (might not work on 486 or lesser CPUs though). It takes 1 second to measure the speed...

Code: Select all

Structure bit64
  LowPart.l
  HighPart.l
EndStructure

Procedure CPUSpeed ()

  DefType.bit64 ulEAX_EDX, ulFreq, ulTicks, ulValue, ulStartCounter, ulResult

  QueryPerformanceFrequency_ (ulFreq)
  QueryPerformanceCounter_ (ulTicks)
   
  ulValue\LowPart = ulTicks\LowPart + ulFreq\LowPart

  ! RDTSC
  
  MOV ulEAX_EDX\LowPart, eax
  MOV ulEAX_EDX\HighPart, edx

  ulStartCounter\LowPart = ulEAX_EDX\LowPart

  While (ulTicks\LowPart <= ulValue\LowPart)
    QueryPerformanceCounter_ (ulTicks)
  Wend

  ! RDTSC

  MOV ulEAX_EDX\LowPart, eax
  MOV ulEAX_EDX\HighPart, edx

  ulResult\LowPart = ulEAX_EDX\LowPart - ulStartCounter\LowPart

  ProcedureReturn ulResult\LowPart / 1000000

EndProcedure

; Takes 1 second to calculate...

mhz = CPUSpeed ()
MessageRequester ("CPU Speed", "CPU speed: " + Str (mhz) + " MHz", #MB_ICONINFORMATION)

--
See ya,
James L Boyd.
http://www.hi-toro.com/
--
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by blueb.

James,
Good one!
I placed the structure "inside" the procedure and
placed the snippet in my UTILITY include file!

Is this going to cause a problem?

Better question is:

"Can structures be placed in procedures and be called from any program, or must
they remain in the top section of the code area?"

Regards,
--blueb
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hi-Toro.

Hi Blue,

It seems to work fine in another program I have, but I haven't checked whether the structure is accessible in other parts of a program...

BTW The code above is a port from C++ -- it's not really my work!


--
See ya,
James L Boyd.
http://www.hi-toro.com/
--
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Structures can be declared anywhere (just before the use)

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by blueb.

Thanks for the answer Fred.

Some languages do not allow this type of
declaration. It must be in the main procedure block.

PB is like a fine wine. It doesn't jump out at
you but you appreciate it more every day. :>D

Regards,
--Bob
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

This snippet looks just like something I needed, but I can't get it to
compile using Vs 3.72.

Maybe I formatted the code wrong.

Does this still work Hi-Toro?

Thanks,
Terry
DominiqueB
Enthusiast
Enthusiast
Posts: 103
Joined: Fri Apr 25, 2003 4:00 pm
Location: France

hello.

Post by DominiqueB »

This code works well for me, didn't you forget to allow assembly in the prefs ?

If yes, what is the error exactly ?

Dominique.
Dominique

Windows 10 64bits. Pure basic 32bits
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

:oops: Had assembly allowed, but just messed up formatting the code
from the unformated post above and couldn't see my goof. Thanks.

I insert the code formatted here for future viewers:

Code: Select all

; from PB forums by Hi-Toro
; post http://jconserv.net/purebasic/viewtopic.php?t=3811&highlight=utility

Structure bit64
  LowPart.l
  HighPart.l
EndStructure 

Procedure CPUSpeed()
  DefType.bit64 ulEAX_EDX, ulFreq, ulTicks, ulValue, ulStartCounter, ulResult
  QueryPerformanceFrequency_(ulFreq)
  QueryPerformanceCounter_(ulTicks)
  ulValue\LowPart = ulTicks\LowPart + ulFreq\LowPart
  
  ! RDTSC
  MOV ulEAX_EDX\LowPart, eax
  MOV ulEAX_EDX\HighPart, edx
  
  ulStartCounter\LowPart = ulEAX_EDX\LowPart
  While (ulTicks\LowPart <= ulValue\LowPart)
    QueryPerformanceCounter_(ulTicks)
  Wend
    
  ! RDTSC
  MOV ulEAX_EDX\LowPart, eax
  MOV ulEAX_EDX\HighPart, edx
  
  ulResult\LowPart = ulEAX_EDX\LowPart - ulStartCounter\LowPart
  ProcedureReturn ulResult\LowPart / 1000000
EndProcedure; Takes 1 second to calculate...
mhz = CPUSpeed()
MessageRequester("CPU Speed", "CPU speed: " + Str (mhz) + " MHz", #MB_ICONINFORMATION)
Thanks to Hi-Toro for this!

Terry
Hi-Toro
Enthusiast
Enthusiast
Posts: 270
Joined: Sat Apr 26, 2003 3:23 pm

Eek!

Post by Hi-Toro »

How did this get dragged up again? :)

It's really just a simple port of some publicly posted code. Aha! Here's where I got it from:

http://www.codeproject.com/csharp/unmanage.asp
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

Does not work on both of my PCs.
Testes on Windows 2000 Latest Servicepack with Pentium IV 2.4 Ghz Notebook and Windows XP Latest Servicepack Pentium IV 2.6GhZ HyperThreaded. Hm, possible I'm to silly to use it. :-)

Cheers
Mike
Tranquil
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

Post by Dreglor »

it returned with -1702mhz hmm
~Dreglor
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

here, too: -1862 Mhz... damn, that's slow.

Or maybe somebody invented something similar to the little-endian thingy:
we don't just write backwards in memory... we calculate backwards :mrgreen:

Timo
quidquid Latine dictum sit altum videtur
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

works perfect on my Athon XP 1800 (real mhz 1600)
with Windows XP Pro :)
Doobrey
Enthusiast
Enthusiast
Posts: 218
Joined: Sat Apr 26, 2003 4:47 am
Location: Dullsville..population: me
Contact:

Post by Doobrey »

Has anyone here tried the DnCPUSpeed() command from my DnMisc library on fast processors?
btw. lib available at
http://www.doobreynet.co.uk/winpb.html
Hi-Toro
Enthusiast
Enthusiast
Posts: 270
Joined: Sat Apr 26, 2003 3:23 pm

Ah...

Post by Hi-Toro »

Yes, I remember now that there were problems with negative values (probably an unsigned/signed thing?). Someone smarter than me will have to fix it though!
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
Post Reply