两个获取CPU频率的代码断

本文介绍两种获取CPU频率的方法:一是通过读取注册表中隐藏的CPU速度信息;二是利用时钟循环与高精度计数器计算非Intel处理器如AMD的频率。提供具体的C++代码实现。

1通过注册表

CString ProcSpeedRead()
{
CString sMHz;
char Buffer[_MAX_PATH];
DWORD BufSize = _MAX_PATH;
DWORD dwMHz = _MAX_PATH;
HKEY hKey;

// open the key where the proc speed is hidden:
long lError = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                           "HARDWARE//DESCRIPTION//System//CentralProcessor//0",
                            0,
                            KEY_READ,
                            &hKey);
   
    if(lError != ERROR_SUCCESS)
      {// if the key is not found, tell the user why:
           FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
                         NULL,
                         lError,
                         0,
                         Buffer,
                         _MAX_PATH,
                         0);
               AfxMessageBox(Buffer);
           return "N/A";
       }

        // query the key:
        RegQueryValueEx(hKey, "~MHz", NULL, NULL, (LPBYTE) &dwMHz, &BufSize);

    // convert the DWORD to a CString:
    sMHz.Format("%i", dwMHz);

return sMHz;
}

2通过时钟循环与高精度计数器计算(非Inter处理器,例如AMD请更改RdTSC宏)

float CGettheProcessorSpeedDlg::ProcSpeedCalc()
{
/*
RdTSC:
It's the Pentium instruction "ReaD Time Stamp Counter". It measures the
number of clock cycles that have passed since the processor was reset, as a
64-bit number. That's what the
_emit lines do.*/
#define RdTSC __asm _emit 0x0f __asm _emit 0x31

// variables for the clock-cycles:
__int64 cyclesStart = 0, cyclesStop = 0;
// variables for the High-Res Preformance Counter:
unsigned __int64 nCtr = 0, nFreq = 0, nCtrStop = 0;


    // retrieve performance-counter frequency per second:
    if(!QueryPerformanceFrequency((LARGE_INTEGER *) &nFreq)) return 0;

    // retrieve the current value of the performance counter:
    QueryPerformanceCounter((LARGE_INTEGER *) &nCtrStop);

    // add the frequency to the counter-value:
    nCtrStop += nFreq;


    _asm
        {// retrieve the clock-cycles for the start value:
            RdTSC
            mov DWORD PTR cyclesStart, eax
            mov DWORD PTR [cyclesStart + 4], edx
        }

        do{// retrieve the value of the performance counter until 1 sec has gone by:
             QueryPerformanceCounter((LARGE_INTEGER *) &nCtr);
          }while (nCtr < nCtrStop);

    _asm
        {// retrieve again the clock-cycles after 1 sec. has gone by:
            RdTSC
            mov DWORD PTR cyclesStop, eax
            mov DWORD PTR [cyclesStop + 4], edx
        }

// stop-start is speed in Hz divided by 1,000,000 is speed in MHz
return    ((float)cyclesStop-(float)cyclesStart) / 1000000;
}

----摘自 CodeProject 作者Thomas Latuske

实例工程下载地址:

http://www.codeproject.com/useritems/Processor_Speed/Processor_Speed_demo.zip

Flash_Tool(V5.1)使用方法: 1、打开升级平台Flash_Tool(V5.1)然后在Download Agent处点击选MTK_ALLnOne_DA.bin文件。 2、点击Scatter File选升级软件自带的scatMTK53_09A_GEMINI.txt文件。随后平台会自动调用BIN文件(升级文件)。 3、第三个是选串口,(具体查看设备管理器里的升级线安装在哪个COM口)。 4、注意Options选项里的Format FAT中选Do Not Format FAT,意思是不要格式化,否则会导致信号弱,改变参数。 5、数据备份设置 点击Options,点选Backup and Restore,选择Backup—Download/Format—Restore。 6、升级:点击Download后,按开机键,首先出现灰色条(备份数据)然后出现红条(初始化),出现蓝条时开始升级,升级完成后出现绿色条(格式化),格式化后出现灰色条(数据还原),最后提示OK。 7、格式化:点击Format后,按开机键,首先出现灰色条(备份数据)然后出现红条(初始化),格式化时出现绿色条,格式化后出现灰色条(数据还原),最后提示OK。 8、读取BIN文件(软件) 找一台开机正常的手机,利用这个平台可将所有MTK手机的软件读取出来,作为其他手机升级之用。 连接好升级线和手机,点击Read Back选项,双击下面的勾选项,会弹出一个窗口,意思是保存的路径。选好保存的路径后,点击Read Back键,按开机键即可读取字库中的数据。 9、Memory Test 字库测试,可查看字库的分区结构,检测字库有无坏区。 按START后,再按开机键即可测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值