您的位置:首页 > 其它

测试CPU品牌和当前工作频率

2011-05-18 00:54 323 查看
这里有一段非常简单的代码,取自网络,我稍加修改,贴在这里。用来检查CPU的生产商和品牌,以及当前工作频率,如果是台式机CPU,频率应该恒定,但是移动版本的CPU,频率不停地在变。以下代码用Visual C++编译,因为内嵌一点汇编,造成移植性变差(例如:GCC汇编跟AT&T汇编语法类似,但是MS汇编跟Intel汇编语法类似),以下代码如果希望在MinGW(GCC)下编译,需要修改那点内嵌的汇编。

#include "stdafx.h"
#ifndef CPUID_HPP_
#define CPUID_HPP_
#include <string>
#include <cstring>
#include <iostream>
#include <windows.h>
typedef unsigned long DWORD;
typedef __int64 LONGLONG;
class CPUID
{
public:
CPUID() : m_eax(0), m_ebx(0), m_ecx(0), m_edx(0){}
std::string vendor(); //哪个厂商出的?Intel, AMD, VIA ?
std::string name();   //这是CPU的整体描述
LONGLONG frequency(DWORD stime = 1000);//CPU的当前工作频率
private:
void  Executecpuid(DWORD veax);
LONGLONG cycles() const
{
DWORD h, l;         //不能直接读取,用变量过渡一下
__asm
{
rdtsc             //把当前CPU周期读入寄存器
mov l, eax
mov h, edx
}
LONGLONG high = h, low = l;
return high << 32 | low;
}
DWORD  m_eax;
DWORD  m_ebx;
DWORD  m_ecx;
DWORD  m_edx;
};
void CPUID::Executecpuid(DWORD veax)
{
DWORD deax;
DWORD debx;
DWORD decx;
DWORD dedx;
__asm
{
mov eax, veax
cpuid
mov deax, eax
mov debx, ebx
mov decx, ecx
mov dedx, edx
}
m_eax = deax;
m_ebx = debx;
m_ecx = decx;
m_edx = dedx;
}
std::string CPUID::vendor()
{
const DWORD S = sizeof(DWORD);
char cVID[S*3+1];
std::memset(cVID, 0, sizeof(cVID));
Executecpuid(0);
std::memcpy(cVID+S*0, &m_ebx, S);
std::memcpy(cVID+S*1, &m_edx, S);
std::memcpy(cVID+S*2, &m_ecx, S);
return std::string(cVID);
}
std::string CPUID::name()
{
const DWORD vendorID = 0x80000002;
const DWORD S = sizeof(DWORD);
char cvendor[S*4*3+1];
std::memset(cvendor, 0, sizeof(cvendor));
for(DWORD i = 0; i < 3; i++)
{
Executecpuid(vendorID + i);
// 每次执行结束后,保存四个寄存器里的ascii码到数组
std::memcpy(cvendor + i*S*4 + S*0, &m_eax, S);
std::memcpy(cvendor + i*S*4 + S*1, &m_ebx, S);
std::memcpy(cvendor + i*S*4 + S*2, &m_ecx, S);
std::memcpy(cvendor + i*S*4 + S*3, &m_edx, S);
}
return std::string(cvendor);
}
LONGLONG CPUID::frequency(DWORD stime)
{
HANDLE hp = GetCurrentProcess();
HANDLE ht = GetCurrentThread();
DWORD pc = GetPriorityClass(hp);
DWORD tp = GetThreadPriority(ht);
BOOL flag1 = FALSE, flag2 = FALSE;
flag1 = SetPriorityClass(hp, REALTIME_PRIORITY_CLASS); //优先级设置最高
flag2 = SetThreadPriority(ht, THREAD_PRIORITY_HIGHEST);//优先级设置最高
//由于CPU本身时间波动较大,因此时间从系统读取,这样比较平均
//周期除以时间就是工作频率,即单位时间内的周期
Sleep(stime);
LARGE_INTEGER fq, st, ed;
QueryPerformanceFrequency(&fq);
QueryPerformanceCounter(&st);
LONGLONG start = cycles();
Sleep(stime);
QueryPerformanceCounter(&ed);
LONGLONG end = cycles();
if(flag1)
SetPriorityClass(hp, pc);
if(flag2)
SetThreadPriority(ht, tp);
CloseHandle(hp);
CloseHandle(ht);
return (end - start) * fq.QuadPart / (ed.QuadPart - st.QuadPart);
}
#endif

#include <cstdio>
#include <cstdlib>
int main()
{
CPUID cpu;
std::printf("Vendor: %s/n", cpu.vendor().c_str());
std::printf("Full name: %s/n", cpu.name().c_str());
const unsigned TIMES = 3;
std::printf("Frequency(testing %d times): ", TIMES);
const double UNIT = 1000.0;
for(unsigned i = 0; i < TIMES; ++i)
{
double fre = cpu.frequency() / UNIT; // 假设CPU主频高于1000Hz
if(fre > UNIT)
fre /= UNIT;
std::printf("%f%s ", fre, (fre > 10.0 ? "MHz" : "GHz"));
}
printf("/n");
std::system("PAUSE");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: