两个获取CPU频率的代码断
来源:互联网 发布:敌在本能寺什么梗 知乎 编辑:程序博客网 时间:2024/04/30 04:42
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
- 两个获取CPU频率的代码断
- 两个获取CPU频率的代码断
- 获取cpu频率的代码
- 获取Android设备的CPU频率
- 获取CPU频率
- 获取cpu频率
- 获取cpu频率
- Windows下获取CPU频率
- c#获取Solaris系统的cpu频率和 利用率
- GPU&CPU频率和工作模式的获取设置
- CPU的时钟频率
- 关于cpu的频率
- 理解CPU的频率
- cpu的几个频率
- CPU的频率
- CPU当前的频率
- CPU的频率
- CPU频率与内存频率的关系
- 占个坑先。打破零蛋很要紧。
- 从Blog起名谈软件需求分析
- CreateProcess使用心得
- 使程序运行一个实例
- 王蓉 - 完美
- 两个获取CPU频率的代码断
- 庆贺开张
- 局域网封QQ
- 神奇代码
- Win2000 Server安全配置入门
- 祝贺, 第一篇blog
- IP MAC捆绑原理及缺陷的讨论
- 取得环境变量
- 由Visual Studio.NET 2003开发的应用程序可以部署到哪些Windows版本上?