Advertisement

detecting processor speed

Started by July 27, 2000 04:17 PM
19 comments, last by Julio 24 years, 4 months ago
Yeah, you can rdtsc to get the number of CPU cycles since power-up. It returns a 64-bit integer in eax and edx:

    LARGE_INTEGER time;_asm rdtsc_asm mov [time],eax_asm mov [time+4], edx    


You can put this in a function and use it to compute how many CPU cycles there are in one second. Note that cpuid and rdtsc only work on Pentium I or later computers, but these days almost everyone has one.



- Democritus

* Truth is universal *

- Democritus * Truth is universal *
Yah, my article has a brief example of Structured Exception handling so you *could* theoretically run this on any computer and if the CPUID command is not supported my instruction handler gets to deal with it rather than the Illegal operation box windows gives. So I can politely say "I have not detected a Pentium Based System" message box and it looks more proffesional and explains the problem rather then the spontaneous termination of my program.

Works well! Mind you all my stuff is in 100% pure assembler, so you''ll have to figure it out in C++ but it''s basically the same cause it''s just a few API calls. But that''ll take care of illegal operations for you, so you don''t have to worry about users not supporting CPUID or RDTSC and such...
- Ben
__________________________Mencken's Law:"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
Advertisement
or u could do it the easy way..use
GetSystemInfo (LPSYSTEM_INFO lpSystemInfo);
which tells u all about your processor the number of processors and all that.

-----------------------------
-cow_in_the_well

http://cowswell.gda.ods.org/


"If it's stupid but works, it's not stupid" - Murphey's Law

- Thomas Cowellwebsite | journal | engine video

Well GetSystemInfo will return some *BASIC* stuff it will not return the speed (Mhz) or other feature of the CPU, and I ''m not sure but I think it''ll only tell you if it is or isn''t an Intel processor not what processor it is. CPUID will return an ID code with a manufacturer ID string in it. Although this ID code can be lied about on Clone systems (Some clones will return "GenuineIntel", even though they are not), it is much better than a yes no answer on intel. Also I belive the original question was about speed only, which GetSystemInfo does not address at all. I think you''ll have to use RDTSC. That''s the only good solution right now, and as that last code listing suggested it''s only 3 asm lines anyhow? That''s gotta be pretty darn simple!
- Ben
__________________________Mencken's Law:"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
If the CPU supports CPUID and RTDSC, try this:

            uint32 GetCpuSpeed(){        int				timeStart	= 0;	int				timeStop	= 0;	unsigned long   StartTicks	= 0;	unsigned long   EndTicks	= 0;	unsigned long   TotalTicks	= 0;	unsigned long   cpuSpeed	= 0;			timeStart = timeGetTime();				// Get tick edge        for( ; ; )	        {		timeStop = timeGetTime();				if ( (timeStop-timeStart) > 1 )		// rollover past 1		{			__asm{				xor    eax, eax					xor    ebx, ebx					xor    ecx, ecx					xor    edx, edx					_emit  0x0f				// CPUID					_emit  0xa2					_emit  0x0f				// RTDSC					_emit  0x31					mov    [StartTicks], eax	// Tick counter starts here			}							break;			}		}		timeStart = timeStop;						for( ; ; )	{		timeStop = timeGetTime();					if ( (timeStop-timeStart) > 1000 )	// one second		{			__asm{				xor    eax, eax					xor    ebx, ebx					xor    ecx, ecx					xor    edx, edx					_emit  0x0f				// CPUID 					_emit  0xa2					_emit  0x0f				// RDTSC					_emit  0x31					mov    [EndTicks], eax			}							break;			}		}		TotalTicks = EndTicks-StartTicks;		// total 		cpuSpeed = TotalTicks/1000000;			// speed		return((uint32)cpuSpeed);}            


Edited by - blazter on July 28, 2000 11:15:02 PM

Edited by - Blazter on July 28, 2000 11:17:19 PM
hey Blatzer, that code works perfectly. but that only works on Pentiums? I''ll try GetSystemInfo for the rest of the stuff that I need. thanks guys.
Joe

JoeMont001@aol.com www.polarisoft.n3.net
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
Advertisement
It works for Athlon processors, too. Not only for Pentiums.

"Everything is relative." -- Even in the world of computers.
everything is relative. -- even in the world of computers... so PUSH BEYOND THE LIMITS OF SANITY!
really? sweet. even better.

JoeMont001@aol.com www.polarisoft.n3.net
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
use GetSystemMetrics()
GetSystemMetrics wont return processor speed i dont belive

+---------------------------------------------
Help me program my 3d game
I just got a new copy of Lear C++ in 21 days!
+---------------------------------------------
Those who dance are considered insane by those who cannot hear the music.

This topic is closed to new replies.

Advertisement