小智 10
我来晚了,但这是我的贡献。我尝试采用更现代的 C++ 方法。
#include <intrin.h> // NOTE this header is MSVC specific!
#include <string>
#include <array>
std::string GetCpuInfo()
{
// 4 is essentially hardcoded due to the __cpuid function requirements.
// NOTE: Results are limited to whatever the sizeof(int) * 4 is...
std::array<int, 4> integerBuffer = {};
constexpr size_t sizeofIntegerBuffer = sizeof(int) * integerBuffer.size();
std::array<char, 64> charBuffer = {};
// The information you wanna query __cpuid for.
// https://learn.microsoft.com/en-us/cpp/intrinsics/cpuid-cpuidex?view=vs-2019
constexpr std::array<int, 3> functionIds = {
// Manufacturer
// EX: "Intel(R) Core(TM"
0x8000'0002,
// Model
// EX: ") i7-8700K CPU @"
0x8000'0003,
// Clockspeed
// EX: " 3.70GHz"
0x8000'0004
};
std::string cpu;
for (int id : functionIds)
{
// Get the data for the current ID.
__cpuid(integerBuffer.data(), id);
// Copy the raw data from the integer buffer into the character buffer
std::memcpy(charBuffer.data(), integerBuffer.data(), sizeofIntegerBuffer);
// Copy that data into a std::string
cpu += std::string(charBuffer.data());
}
return cpu;
}
Run Code Online (Sandbox Code Playgroud)
以下是我自己执行该函数的结果:“Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz”
老实说,像这样的东西还没有标准化真的很烦人......
以下是在Windows计算机上获取所需信息的一种方法.我从一个实际的项目中复制并粘贴了一些微小的修改,所以请随意清理它以使其更有意义.
int CPUInfo[4] = {-1};
unsigned nExIds, i = 0;
char CPUBrandString[0x40];
// Get the information associated with each extended ID.
__cpuid(CPUInfo, 0x80000000);
nExIds = CPUInfo[0];
for (i=0x80000000; i<=nExIds; ++i)
{
__cpuid(CPUInfo, i);
// Interpret CPU brand string
if (i == 0x80000002)
memcpy(CPUBrandString, CPUInfo, sizeof(CPUInfo));
else if (i == 0x80000003)
memcpy(CPUBrandString + 16, CPUInfo, sizeof(CPUInfo));
else if (i == 0x80000004)
memcpy(CPUBrandString + 32, CPUInfo, sizeof(CPUInfo));
}
//string includes manufacturer, model and clockspeed
cout << "CPU Type: " << CPUBrandString << endl;
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);
cout << "Number of Cores: " << sysInfo.dwNumberOfProcessors << endl;
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx(&statex);
cout << "Total System Memory: " << (statex.ullTotalPhys/1024)/1024 << "MB" << endl;
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅GetSystemInfo,GlobalMemoryStatusEx和__cpuid.虽然我没有包含它,但您还可以通过GetSystemInfo函数确定操作系统是32位还是64位.
在Windows上确定CPU时钟速度:
double CPUSpeed()
{
wchar_t 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,
L"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);
wprintf(Buffer);
return 0;
}
// query the key:
RegQueryValueEx(hKey, L"~MHz", NULL, NULL, (LPBYTE) &dwMHz, &BufSize);
return (double)dwMHz;
}
Run Code Online (Sandbox Code Playgroud)
CPU很简单。使用cpuid
说明。我将留下其他海报来寻找一种可移植的方法来确定系统有多少RAM。:-)
对于特定于Linux的方法,您可以访问/proc/meminfo
(和/proc/cpuinfo
,如果不麻烦解析cpuid
响应的话)。
对于带有GCC的Linux,您可以使用Windows之类的非常相似的解决方案。您需要包含,<cpuid.h>
并且需要__cpuid()
基于this来修改方法的输入。
#include <cpuid.h>
char CPUBrandString[0x40];
unsigned int CPUInfo[4] = {0,0,0,0};
__cpuid(0x80000000, CPUInfo[0], CPUInfo[1], CPUInfo[2], CPUInfo[3]);
unsigned int nExIds = CPUInfo[0];
memset(CPUBrandString, 0, sizeof(CPUBrandString));
for (unsigned int i = 0x80000000; i <= nExIds; ++i)
{
__cpuid(i, CPUInfo[0], CPUInfo[1], CPUInfo[2], CPUInfo[3]);
if (i == 0x80000002)
memcpy(CPUBrandString, CPUInfo, sizeof(CPUInfo));
else if (i == 0x80000003)
memcpy(CPUBrandString + 16, CPUInfo, sizeof(CPUInfo));
else if (i == 0x80000004)
memcpy(CPUBrandString + 32, CPUInfo, sizeof(CPUInfo));
}
cout << "CPU Type: " << CPUBrandString << endl;
Run Code Online (Sandbox Code Playgroud)