如何在VC中用CPUID获取CPU当前运行的真实时钟频率啊?

chi198199 2004-10-31 06:46:38
rt
...全文
477 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
oyljerry 2004-11-01
  • 打赏
  • 举报
回复
用cpuid在一段的时间内计算
magicgod79 2004-11-01
  • 打赏
  • 举报
回复
HyperThreading CPU无法使用QueryPerformanceFrequency来得出即时频率!!!
kugou123 2004-10-31
  • 打赏
  • 举报
回复
用RDTSC指令:

#include <STDIO.H>
#include <WINDOWS.H>

void main( void )
{
unsigned long ProcSpeed = 0;

HKEY hKey;
LONG rt = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &hKey);
if( ERROR_SUCCESS == rt )
{
unsigned long buflen = sizeof( ProcSpeed );
RegQueryValueEx( hKey, "~MHz", NULL, NULL, (LPBYTE)&ProcSpeed, &buflen );
RegCloseKey(hKey);
}

if( 0 != ProcSpeed )
printf( "%ldMHz\n", ProcSpeed );
else
printf( "查询失败\n", ProcSpeed );
}

////// raw speed
#include <WINDOWS.H>
#include <STDIO.H>
typedef unsigned long ulong;

// 高精度计时
inline unsigned __int64 GetCycleCount()
{
// __asm RDTSC
__asm _emit 0x0F
__asm _emit 0x31
// return EDX:EAX;
}

void main( void )
{
__int64 raw_freq = 0;
__int64 t0,t1;
__int64 freq =0; // Most current frequ. calculation
__int64 freq2 =0; // 2nd most current frequ. calc.
__int64 freq3 =0; // 3rd most current frequ. calc.
__int64 total; // Sum of previous three frequency calculations
__int64 tries=0; // Number of times a calculation has

__int64 total_cycles=0, cycles; // Clock cycles elapsed during test
__int64 stamp0=0, stamp1=0; // Time Stamp Variable for beginning and end of test
__int64 total_ticks=0, ticks; // Microseconds elapsed during test
__int64 count_freq; // 高精度计数器频率

#ifdef WIN32
int iPriority;
HANDLE hThread = GetCurrentThread();
#endif

if( !QueryPerformanceFrequency( (PLARGE_INTEGER)&count_freq ) )
{
printf( "ERROR: The installed hardware does not support a high-resolution performance counter\n" );
return;
}

do { // This do loop runs up to 20 times or
// until the average of the previous
// three calculated frequencies is
// within 1 MHz of each of the
// individual calculated frequencies.
// This resampling increases the
// accuracy of the results since
// outside factors could affect this
// calculation

tries++; // Increment number of times sampled on this call to cpuspeed
freq3 = freq2; // Shift frequencies back to make room for new frequency measurement
freq2 = freq; //

QueryPerformanceCounter( (PLARGE_INTEGER)&t0 );
t1 = t0;

#ifdef WIN32
iPriority = GetThreadPriority(hThread);
if ( iPriority != THREAD_PRIORITY_ERROR_RETURN )
{
SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
}
#endif

while( t1-t0 < 50 )
{
// Loop until 50 ticks have
// passed since last read of hi-
// res counter. This accounts for
// overhead later.

QueryPerformanceCounter( (PLARGE_INTEGER)&t1 );
stamp0 = GetCycleCount();
}
t0 = t1; // Reset Initial Time

while( t1-t0 < 1000 )
{
// Loop until 1000 ticks have
// passed since last read of hi-
// res counter. This allows for
// elapsed time for sampling.

QueryPerformanceCounter( (PLARGE_INTEGER)&t1 );
stamp1 = GetCycleCount();
}

#ifdef WIN32
// Reset priority
if ( iPriority != THREAD_PRIORITY_ERROR_RETURN )
{
SetThreadPriority(hThread, iPriority);
}
#endif // WIN32

cycles = stamp1 - stamp0;// Number of internal
// clock cycles is
// difference between
// two time stamp
// readings.
ticks = t1 - t0;
// Number of external ticks is
// difference between two
// hi-res counter reads.

// Note that some seemingly arbitrary mulitplies and
// divides are done below. This is to maintain a
// high level of precision without truncating the
// most significant data. According to what value
// ITERATIIONS is set to, these multiplies and
// divides might need to be shifted for optimal
// precision.

ticks = ticks * 100000;
// Convert ticks to hundred
// thousandths of a tick

ticks = ticks / ( count_freq/10 );
// Hundred Thousandths of a
// Ticks / ( 10 ticks/second )
// = microseconds (us)

total_ticks += ticks;
total_cycles += cycles;

if( ticks%count_freq > count_freq/2 )
ticks++; // Round up if necessary

freq = cycles/ticks; // Cycles / us = MHz

if( cycles%ticks > ticks/2 )
freq++; // Round up if necessary

total = ( freq + freq2 + freq3 );
// Total last three frequency
// calculations

} while ( (tries < 3 ) ||
(tries < 20)&&
((abs(3 * freq -total) > 3 )||
(abs(3 * freq2-total) > 3 )||
(abs(3 * freq3-total) > 3 )));
// Compare last three calculations to
// average of last three calculations.

// Try one more significant digit.
freq3 = ( total_cycles * 10 ) / total_ticks;
freq2 = ( total_cycles * 100 ) / total_ticks;

if ( freq2 - (freq3 * 10) >= 6 )
freq3++;

raw_freq = total_cycles / total_ticks;
printf( "%ldMHz\n", raw_freq );
}

chi198199 2004-10-31
  • 打赏
  • 举报
回复
谢谢楼上的,但是我要真实的,非常准确的.

一定要用cpuid指令来读取,
eastnofail 2004-10-31
  • 打赏
  • 举报
回复
有关cpu的信息都在注册表的键值:
HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0

读注册表即可

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧