How do you detect the number of physical processors/cores in .net?
Open Device Manager (in the search box of the taskbar, type in "Device Manager", then select Open) Click on ">" to expand the Processors section. Count the number of entries to get the number of logical processors.
These days, most new PCs have dual-core central processors (CPU). That's one chip with two complete microprocessors on it, both sharing one path to memory and peripherals. If you have a high-end gaming PC or a workstation, you might have one or two processor chips with four cores each.
Quick Answer. A processor count is a measure of how many cores a CPU possesses. Generally, a higher number of processors means that your computer can handle more tasks concurrently. Most CPUs hold four or six cores. Although, eight or more is best if you plan on editing high-quality video or streaming games.
System.Environment.ProcessorCount
returns the number of logical processors
http://msdn.microsoft.com/en-us/library/system.environment.processorcount.aspx
For physical processor count you'd probably need to use WMI - the following metadata is supported in XP/Win2k3 upwards (Functionality enabled in SP's prior to Vista/Win2k8).
Win32_ComputerSystem.NumberOfProcessors returns physical count
Win32_ComputerSystem.NumberOfLogicalProcessors returns logical (duh!)
Be cautious that HyperThreaded CPUs appear identical to multicore'd CPU's yet the performance characteristics are very different.
To check for HT-enabled CPUs examine each instance of Win32_Processor and compare these two properties.
Win32_Processor.NumberOfLogicalProcessors
Win32_Processor.NumberOfCores
On multicore systems these are typically the same the value.
Also, be aware of systems that may have multiple Processor Groups, which is often seen on computers with a large number of processors. By default .Net will only using the first processor group - which means that by default, threads will utilize only CPUs from the first processor group, and Environment.ProcessorCount
will return only the number of CPUs in this group. According to Alastair Maw's answer, this behavior can be changed by altering the app.config as follows:
<configuration> <runtime> <Thread_UseAllCpuGroups enabled="true"/> <GCCpuGroup enabled="true"/> <gcServer enabled="true"/> </runtime> </configuration>
While Environment.ProcessorCount
will indeed get you the number of virtual processors in the system, that may not be the number of processors available to your process. I whipped up a quick little static class/property to get exactly that:
using System; using System.Diagnostics; /// <summary> /// Provides a single property which gets the number of processor threads /// available to the currently executing process. /// </summary> internal static class ProcessInfo { /// <summary> /// Gets the number of processors. /// </summary> /// <value>The number of processors.</value> internal static uint NumberOfProcessorThreads { get { uint processAffinityMask; using (var currentProcess = Process.GetCurrentProcess()) { processAffinityMask = (uint)currentProcess.ProcessorAffinity; } const uint BitsPerByte = 8; var loop = BitsPerByte * sizeof(uint); uint result = 0; while (--loop > 0) { result += processAffinityMask & 1; processAffinityMask >>= 1; } return (result == 0) ? 1 : result; } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With