BlackWaspTM
System Information
.NET 1.1+

Getting the Processor Speed and Number of Cores

Some types of software need to determine the number of processors or cores present and to know the clock speed. This information could be displayed to the user or can be used to adjust the user experience according to the performance of the machine.

Windows Management Instrumentation

In some projects you may wish to obtain the number and speed of the processors or cores present in a computer. For software that presents system information you may display this data in a textual or graphical format. For programs that have a complex, CPU-intensive user interface (UI), you may decide to degrade the quality of the UI on slower computers to improve the response speed and ensure the best user experience.

There are several ways in which you can determine the number of processors and their speed. In this article we will use Windows Management Instrumentation (WMI), returning all of the required information by executing a single WMI Query Language (WQL) query. As we will be using WMI, you need to add a reference to the System.Management assembly. As the classes we need are in the System.Management namespace, you should also add the following using directive to simplify the code:

using System.Management;

Win32_Processor

We will obtain the processor details using the Win32_Processor WMI class. This class is used to represent a single processor in a computer. When performing the query we will obtain one result for each physical processor that is present. We'll then loop through the results and output the values to the console.

To read the data into a ManagementObjectCollection, add the code shown below. In this case the query uses "SELECT *" to obtain all of the Win32_Processor information. This includes many additional items to those that we require. You could add a column list instead of the asterisk (*) to restrict the amount of information returned.

ObjectQuery wql = new ObjectQuery("SELECT * FROM Win32_Processor");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
ManagementObjectCollection results = searcher.Get();

There are five values in the result set that are of interest when determining each processor's potential performance. These are:

  • NumberOfCores. Gives the number of physical cores in the processor. For example, a quad-core processor will give the result, 4.
  • NumberOfLogicalProcessors. Gives the number of logical processors or cores in the processor. This can differ from the NumberOfCores value if, for example, the processor supports hyper-threading.
  • CurrentClockSpeed. Gives the current clock speed of the processor in megahertz (MHz). This is the speed at the time that the WMI query is executed. It can yield a lower result than expected if the processor has slowed to save power.
  • MaxClockSpeed. Gives the maximum clock speed supported by the processor in MHz.
  • ExtClock. Gives the external clock speed for the processor in MHz.

To display the results from the ManagementObjectCollection, add the following loop to the program:

foreach (ManagementObject result in results)
{
    Console.WriteLine("NumberOfCores : {0}", result["NumberOfCores"]);
    Console.WriteLine("NumberOfLogicalProcessors : {0}",
        result["NumberOfLogicalProcessors"]);
    Console.WriteLine("CurrentClockSpeed : {0}MHz", result["CurrentClockSpeed"]);
    Console.WriteLine("MaxClockSpeed : {0}MHz", result["MaxClockSpeed"]);
    Console.WriteLine("ExtClock : {0}MHz", result["ExtClock"]);
}

The results will vary according to the computer that executes the code. The example output below was generated using a computer with a 1.9GHz Athlon 64 X2 processor.

NumberOfCores : 2
NumberOfLogicalProcessors : 2
CurrentClockSpeed : 1900MHz
MaxClockSpeed : 1900MHz
ExtClock : 200MHz
10 August 2008