如何通过.NET/C# 查找CPU 核心数?
有没有办法通过.NET/C#找出CPU核心数?
PS这是一个直接的代码问题,而不是“我应该使用多线程吗?”问题! :-)
Is there a way via .NET/C# to find out the number of CPU cores?
PS This is a straight code question, not a "Should I use multi-threading?" question! :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以获得与处理器相关的多种不同信息:
这些都可以不同;如果机器具有 2 个支持超线程的双核处理器,则有 2 个物理处理器、4 个核心和 8 个逻辑处理器。
逻辑处理器的数量可通过 Environment 类获得,但其他信息只能通过 WMI 获得(并且您可能需要安装一些修补程序或服务包以在某些系统上获取它):
确保在项目中添加对 System.Management.dll 的引用
在 .NET Core 中,它作为 NuGet 包提供(仅适用于 Windows)。
物理处理器:
核心:
逻辑处理器:
或
从 Windows 中排除的处理器:
您还可以在setupapi.dll 用于发现已从 Windows 中排除(例如通过启动设置)并且使用上述方法无法检测到的处理器。下面的代码给出了现有逻辑处理器的总数(我一直无法弄清楚如何区分物理处理器和逻辑处理器),包括那些已从 Windows 中排除的处理器:
There are several different pieces of information relating to processors that you could get:
These can all be different; in the case of a machine with 2 dual-core hyper-threading-enabled processors, there are 2 physical processors, 4 cores, and 8 logical processors.
The number of logical processors is available through the Environment class, but the other information is only available through WMI (and you may have to install some hotfixes or service packs to get it on some systems):
Make sure to add a reference in your project to System.Management.dll
In .NET Core, this is available (for Windows only) as a NuGet package.
Physical Processors:
Cores:
Logical Processors:
OR
Processors excluded from Windows:
You can also use Windows API calls in setupapi.dll to discover processors that have been excluded from Windows (e.g. through boot settings) and aren't detectable using the above means. The code below gives the total number of logical processors (I haven't been able to figure out how to differentiate physical from logical processors) that exist, including those that have been excluded from Windows:
[文档]
[Documentation]
WMI 查询速度很慢,因此请尝试仅选择所需的成员,而不是使用 Select *。
下面的查询需要 3.4 秒:
而这个查询需要 0.122 秒:
WMI queries are slow, so try to Select only the desired members instead of using Select *.
The following query takes 3.4s:
While this one takes 0.122s:
Environment.ProcessorCount 应该为您提供内核数量本地机器。
Environment.ProcessorCount should give you the number of cores on the local machine.
最简单的方法=
Environment.ProcessorCount
示例来自 Environment.ProcessorCount 属性
The the easyest way =
Environment.ProcessorCount
Exemple from Environment.ProcessorCount Property
至少可以说,了解 .NET 如何在内部实现这一点是相当有趣的……它就像下面这样“简单”:
It's rather interesting to see how .NET get this internally to say the least... It's as "simple" as below:
来自 .NET Framework 源代码
您还可以通过
Kernel32.dll
上的 PInvoke 获取它以下代码或多或少来自
SystemInfo.cs
System.Web 源代码位于此处:From .NET Framework source
You can also get it with PInvoke on
Kernel32.dll
The following code is coming more or less from
SystemInfo.cs
from System.Web source located here:这里已经有很多答案了,但有些答案得到了很多支持,而且是不正确的。
如果您的系统 WMI 配置不正确,.NET Environment.ProcessorCount 将返回不正确的值,并且可能会严重失败。
如果您想要一种可靠的方法来计算内核数,唯一的方法是 Win32 API。
这是一个 C++ 片段:
由于这是一个 .NET C# 问题,因此这是移植版本:
There are many answers here already, but some have heavy upvotes and are incorrect.
The .NET Environment.ProcessorCount WILL return incorrect values and can fail critically if your system WMI is configured incorrectly.
If you want a RELIABLE way to count the cores, the only way is Win32 API.
Here is a C++ snippet:
And since this is a .NET C# Question, here's the ported version:
一种选择是从注册表中读取数据。
有关该主题的 MSDN 文章: http://msdn.microsoft.com/en-us/library/microsoft.win32.registry.localmachine(v=vs.71).aspx)
我相信处理器可以位于此处,HKEY_LOCAL_MACHINE\ HARDWARE\DESCRIPTION\System\CentralProcessor
我有理由确信大多数系统上都会有该注册表项。
我想我应该投入 0.02 美元。
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)
The processors, I believe can be located here, HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor
I am reasonably sure the registry entry will be there on most systems.
Thought I would throw my $0.02 in.
你可以使用这个类:
You can use this class:
我一直在寻找同样的东西,但我不想安装任何 nuget 或 servicepack,所以我找到了这个解决方案,它非常简单直接,
我想使用这个讨论运行 WMIC 命令并获取该值非常容易,这是 C# 代码。您只需要使用 System.Management 命名空间(以及用于进程等的更多标准命名空间)。
I was looking for the same thing but I don't want to install any nuget or servicepack, so I found this solution, it is pretty simple and straight forward,
using this discussion, I thought it would be so easy to run that WMIC command and get that value, here is the C# code. You only need to use System.Management namespace (and couple more standard namespaces for process and so on).