|  | 
 
 发表于 2004-2-19 09:55:00
|
显示全部楼层 
Re:请教一个初级问题。,望DX们帮忙
| int GetCPUSpeed( void ) {
 DWORD                        dwFeatures;
 DWORD                        dwStartTime;
 DWORD                        dwStopTime;
 __int64                        i64StartTicks;
 __int64                        i64EndTicks;
 __int64                        i64TotalTicks;
 int                                iSpeed;
 
 
 // Get the capabilities of the cpu.
 dwFeatures = GetCPUCaps();
 
 // The rdtsc instruction must be available.
 if ( !( dwFeatures & CPU_FEATURE_RDTSC ) )
 return -1;
 
 
 // Get the start time.
 dwStartTime = timeGetTime();
 
 //
 // Wait for a new millisecond to start.
 //
 do
 {
 dwStopTime = timeGetTime();
 }
 while ( ( dwStopTime - dwStartTime ) == 0 );
 
 // Use the new start time.
 dwStartTime = dwStopTime;
 
 
 //
 // Get the start tick count.
 //
 __asm
 {
 //
 // Save registers.
 //
 push        eax
 push        ebx
 push        edx
 
 //
 // Get the tick count.
 //
 rdtsc
 lea                ebx, i64StartTicks
 mov                [ebx], eax
 mov                [ebx + 4], edx
 
 //
 // Restore registers.
 //
 pop                edx
 pop                ebx
 pop                eax
 }
 
 
 //
 // Loop till time? up.
 //
 while ( true )
 {
 // If one second is over ...
 if ( ( timeGetTime() - dwStartTime ) >= 1000 )
 {
 //
 // ... get the end tick count.
 //
 __asm
 {
 //
 // Save registers.
 //
 push        eax
 push        ebx
 push        edx
 
 //
 // Get the tick count.
 //
 rdtsc
 lea                ebx, i64EndTicks
 mov                [ebx], eax
 mov                [ebx + 4], edx
 
 //
 // Restore registers.
 //
 pop                edx
 pop                ebx
 pop                eax
 }
 
 break;
 }
 }
 
 // Calculate the difference in clock ticks.
 i64TotalTicks = i64EndTicks - i64StartTicks;
 
 // Calculate the speed in Mhz.
 iSpeed = ( int ) ( i64TotalTicks / 1000000 );
 
 // Return the speed in Mhz.
 return iSpeed;
 }
 
 /*
 * Description:                Retrieves the cpu features that the processor supports.
 *
 * Parameters:                None.
 *
 * Return value:        A combination of the following flags:
 *
 *                                        CPU_FEATURE_MMX   - The MMX instruction set is available.
 *                                        CPU_FEATURE_RDTSC - The rdtsc instruction is available.
 *                                        CPU_FEATURE_3DNOW - The 3DNow! instruction set is available.
 *
 */
 DWORD GetCPUCaps( void )
 {
 SYSTEM_INFO                si;
 DWORD                        dwFeatures;
 
 
 // Assume no features are present.
 dwFeatures = 0;
 
 // Get the system information.
 GetSystemInfo(&si);
 
 // If this is at least a pentium or compatibel ...
 if ( si.dwProcessorType != PROCESSOR_INTEL_386 &&
 si.dwProcessorType != PROCESSOR_INTEL_486)
 {
 //
 // ... get the features.
 //
 __asm
 {
 //
 // Save registers.
 //
 push        eax
 push        ebx
 push        ecx
 push        edx
 
 //
 // Get the features.
 //
 mov                eax, 1
 cpuid
 mov                dwFeatures, edx
 
 //
 // Restore registers.
 //
 pop                edx
 pop                ecx
 pop                ebx
 pop                eax
 }
 }
 
 // Return the features.
 return dwFeatures;
 }
 | 
 |