Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get CPU frequency in c#

Tags:

c#

cpu

frequency

How can I get in c# the CPU frequency (example : 2Ghz) ? It's simple but I don't find it in the environnement variables. Thanks :)

like image 943
Orpheo Avatar asked Aug 03 '11 08:08

Orpheo


People also ask

How do I find the frequency of my CPU?

Clock speed (also “clock rate” or “frequency”) is one of the most significant. If you're wondering how to check your clock speed, click the Start menu (or click the Windows* key) and type “System Information.” Your CPU's model name and clock speed will be listed under “Processor”.

What is CPU operating frequency?

A CPU's clock speed represents how many cycles per second it can execute. Clock speed is also referred to as clock rate, PC frequency and CPU frequency. This is measured in gigahertz, which refers to billions of pulses per second and is abbreviated as GHz.


3 Answers

If you want to get the turbo speed, you can make use of the "% Processor Performance" performance counter and multiply it with the WMI "MaxClockSpeed" as follows:

private string GetCPUInfo()
{
  PerformanceCounter cpuCounter = new PerformanceCounter("Processor Information", "% Processor Performance", "_Total");
  double cpuValue = cpuCounter.NextValue();

  Thread loop = new Thread(() => InfiniteLoop());
  loop.Start();

  Thread.Sleep(1000);
  cpuValue = cpuCounter.NextValue();
  loop.Abort();

  foreach (ManagementObject obj in new ManagementObjectSearcher("SELECT *, Name FROM Win32_Processor").Get())
  {
    double maxSpeed = Convert.ToDouble(obj["MaxClockSpeed"]) / 1000;
    double turboSpeed = maxSpeed * cpuValue / 100;
    return string.Format("{0} Running at {1:0.00}Ghz, Turbo Speed: {2:0.00}Ghz",  obj["Name"], maxSpeed, turboSpeed);
  }

  return string.Empty;
}

The InfiniteLoop method is simply an integer that gets 1 added and subtracted:

private void InfiniteLoop()
{
  int i = 0;

  while (true)
    i = i + 1 - 1;
}

The InfiniteLoop method is just added to give the CPU something to do and turbo in the process. The loop is allowed to run for a second before the next value is taken and the loop aborted.

like image 126
RooiWillie Avatar answered Oct 05 '22 22:10

RooiWillie


One could take the information out of the registry, but dunno if it works on Windows XP or older (mine is Windows 7).

HKEY_LOCAL_MACHINE/HARDWARE/DESCRIPTION/CentralProcessor/0/ProcessorName 

reads like

Intel(R) Core(TM)2 Quad CPU    Q6600  @ 2.40GHz

for me.

Something like this code could retrieve the information (not tested):

RegistryKey processor_name = Registry.LocalMachine.OpenSubKey(@"Hardware\Description\System\CentralProcessor\0", RegistryKeyPermissionCheck.ReadSubTree);  


if (processor_name != null)
{
  if (processor_name.GetValue("ProcessorNameString") != null)
  {
    string value = processor_name.GetValue("ProcessorNameString");
    string freq = value.Split('@')[1];
    ...
  }
}

(source: here)

like image 32
Matten Avatar answered Oct 05 '22 22:10

Matten


You can get it via WMI, but it's quite slow so if you're going to be getting it on more than one occasion I'd suggest you cache it - something like:

namespace Helpers
{
    using System.Management;

    public static class HardwareHelpers
    {
        private static uint? maxCpuSpeed = null;
        public static uint MaxCpuSpeed
        {
            get
            {
                return maxCpuSpeed.HasValue ? maxCpuSpeed.Value : (maxCpuSpeed = GetMaxCpuSpeed()).Value;
            }
        }

        private static uint GetMaxCpuSpeed()
        {
            using (var managementObject = new ManagementObject("Win32_Processor.DeviceID='CPU0'"))
            {
                var sp = (uint)(managementObject["MaxClockSpeed"]);

                return sp;
            }
        }
    }
}
like image 44
Steven Robbins Avatar answered Oct 05 '22 23:10

Steven Robbins