Page 1 of 2

Posted: Thu Sep 12, 2002 1:15 am
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/
--

Posted: Thu Sep 12, 2002 2:30 am
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

Posted: Thu Sep 12, 2002 7:06 am
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/
--

Posted: Thu Sep 12, 2002 7:39 am
by BackupUser
Restored from previous forum. Originally posted by fred.

Structures can be declared anywhere (just before the use)

Fred - AlphaSND

Posted: Thu Sep 12, 2002 1:20 pm
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

Posted: Mon Sep 22, 2003 9:50 pm
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

hello.

Posted: Mon Sep 22, 2003 9:56 pm
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.

Posted: Mon Sep 22, 2003 10:05 pm
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

Eek!

Posted: Wed Sep 24, 2003 10:34 am
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

Posted: Wed Sep 24, 2003 8:30 pm
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

Posted: Wed Sep 24, 2003 8:35 pm
by Dreglor
it returned with -1702mhz hmm

Posted: Wed Sep 24, 2003 8:39 pm
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

Posted: Wed Sep 24, 2003 11:48 pm
by jack
works perfect on my Athon XP 1800 (real mhz 1600)
with Windows XP Pro :)

Posted: Thu Sep 25, 2003 12:45 am
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

Ah...

Posted: Thu Sep 25, 2003 10:10 am
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!