本文翻译自:How to find the Number of CPU Cores via .NET/C#?
Is there a way via .NET/C# to find out the number of CPU cores? 是否可以通过.NET / C#找出CPU内核数?
PS This is a straight code question, not a "Should I use multi-threading?" PS:这是一个直接的代码问题,而不是“我应该使用多线程吗?” question! 题! :-) :-)
#1楼
参考:https://stackoom.com/question/6TCP/如何通过-NET-C-查找CPU内核数
#2楼
Environment.ProcessorCount
#3楼
Environment.ProcessorCount应该为您提供本地计算机上的内核数。
#4楼
WMI queries are slow, so try to Select only the desired members instead of using Select *. WMI查询的速度很慢,因此请尝试仅选择所需的成员,而不要使用Select *。
The following query takes 3.4s: 以下查询耗时3.4秒:
foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_Processor").Get())
While this one takes 0.122s: 虽然这个花费了0.122s:
foreach (var item in new System.Management.ManagementObjectSearcher("Select NumberOfCores from Win32_Processor").Get())
#5楼
The following program prints the logical and physical cores of a windows machine. 以下程序打印Windows机器的逻辑和物理核心。
#define STRICT
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <omp.h>
template<typename T>
T *AdvanceBytes(T *p, SIZE_T cb)
{
return reinterpret_cast<T*>(reinterpret_cast<BYTE *>(p) + cb);
}
class EnumLogicalProcessorInformation
{
public:
EnumLogicalProcessorInformation(LOGICAL_PROCESSOR_RELATIONSHIP Relationship)
: m_pinfoBase(nullptr), m_pinfoCurrent(nullptr), m_cbRemaining(0)
{
DWORD cb = 0;
if (GetLogicalProcessorInformationEx(Relationship,
nullptr, &cb)) return;
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return;
m_pinfoBase =
reinterpret_cast<SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *>
(LocalAlloc(LMEM_FIXED, cb));
if (!m_pinfoBase) return;
if (!GetLogicalProcessorInformationEx(Relationship,
m_pinfoBase, &cb)) return;
m_pinfoCurrent = m_pinfoBase;
m_cbRemaining = cb;
}
~EnumLogicalProcessorInformation() { LocalFree(m_pinfoBase); }
void MoveNext()
{
if (m_pinfoCurrent) {
m_cbRemaining -= m_pinfoCurrent->Size;
if (m_cbRemaining) {
m_pinfoCurrent = AdvanceBytes(m_pinfoCurrent,
m_pinfoCurrent->Size);
} else {
m_pinfoCurrent = nullptr;
}
}
}
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *Current()
{ return m_pinfoCurrent; }
private:
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *m_pinfoBase;
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *m_pinfoCurrent;
DWORD m_cbRemaining;
};
int __cdecl main(int argc, char **argv)
{
int numLogicalCore = 0;
int numPhysicalCore = 0;
for (EnumLogicalProcessorInformation enumInfo(RelationProcessorCore);
auto pinfo = enumInfo.Current(); enumInfo.MoveNext())
{
int numThreadPerCore = (pinfo->Processor.Flags == LTP_PC_SMT) ? 2 : 1;
// std::cout << "thread per core: "<< numThreadPerCore << std::endl;
numLogicalCore += numThreadPerCore;
numPhysicalCore += 1;
}
printf ("Number of physical core = %d , Number of Logical core = %d \n", numPhysicalCore, numLogicalCore );
char c = getchar(); /* just to wait on to see the results in the command prompt */
return 0;
}
/*
I tested with Intel Xeon four cores with hyper threading and here is the result
Number of physical core = 4 , Number of Logical core = 8
*/
#6楼
One option would be to read the data from the registry. 一种选择是从注册表中读取数据。 MSDN Article On The Topic: http://msdn.microsoft.com/en-us/library/microsoft.win32.registry.localmachine(v=vs.71).aspx ) 有关该主题的MSDN文章: http : //msdn.microsoft.com/zh-cn/library/microsoft.win32.registry.localmachine (v= vs.71 ) .aspx )
The processors, I believe can be located here, HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor 我相信处理器可以位于此处,HKEY_LOCAL_MACHINE \\ HARDWARE \\ DESCRIPTION \\ System \\ CentralProcessor
private void determineNumberOfProcessCores()
{
RegistryKey rk = Registry.LocalMachine;
String[] subKeys = rk.OpenSubKey("HARDWARE").OpenSubKey("DESCRIPTION").OpenSubKey("System").OpenSubKey("CentralProcessor").GetSubKeyNames();
textBox1.Text = "Total number of cores:" + subKeys.Length.ToString();
}
I am reasonably sure the registry entry will be there on most systems. 我有理由确定,大多数系统上都将存在注册表项。
Though I would throw my $0.02 in. 尽管我会投入0.02美元。