Dot-Net

檢測處理器數量

  • October 9, 2008

您如何檢測 .net 中的物理處理器/核心的數量?

System.Environment.ProcessorCount

返回邏輯處理器的數量

http://msdn.microsoft.com/en-us/library/system.environment.processorcount.aspx

對於物理處理器數量,您可能需要使用 WMI - XP/Win2k3 以上支持以下元數據(在 Vista/Win2k8 之前的 SP 中啟用了功能)。

Win32_ComputerSystem.NumberOfProcessors返回物理計數

Win32_ComputerSystem.NumberOfLogicalProcessors返回邏輯(呃!)

請注意,超執行緒 CPU 看起來與多核 CPU 相同,但性能特徵卻大不相同

要檢查啟用了 HT 的 CPU,請檢查 Win32_Processor 的每個實例並比較這兩個屬性。

Win32_Processor.NumberOfLogicalProcessors

Win32_Processor.NumberOfCores

在多核系統上,這些值通常是相同的。

此外,請注意可能具有多個處理器組的系統,這在具有大量處理器的電腦上很常見。預設情況下, .Net 將僅使用第一個處理器組- 這意味著預設情況下,執行緒將僅使用第一個處理器組中的 CPU,並且Environment.ProcessorCount僅返回該組中的 CPU 數量。根據Alastair Maw 的回答,可以通過如下更改 app.config 來更改此行為:

<configuration>
  <runtime>
     <Thread_UseAllCpuGroups enabled="true"/>
     <GCCpuGroup enabled="true"/>
     <gcServer enabled="true"/>
  </runtime>
</configuration>

引用自:https://stackoverflow.com/questions/188503