简体   繁体   English

如何在Windows 64位上检测CPU速度?

[英]How to detect CPU speed on Windows 64bit?

I've found the following code from here " 我从这里找到了以下代码“ http://www.boyet.com/Articles/CodeFromInternet.html ". http://www.boyet.com/Articles/CodeFromInternet.html ”。
It returns the speed of the CPU in GHz but works only on 32bit Windows. 它返回以GHz为单位的CPU速度,但仅在32位Windows上有效。

using System;
using System.Management;

namespace CpuSpeed
{
    class Program
    {
        static double? GetCpuSpeedInGHz()
        {
            double? GHz = null;
            using (ManagementClass mc = new ManagementClass("Win32_Processor"))
            {
                foreach (ManagementObject mo in mc.GetInstances())
                {
                    GHz = 0.001 * (UInt32) mo.Properties["CurrentClockSpeed"].Value;
                    break;
                }
            }
            return GHz;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("The current CPU speed is {0}", (GetCpuSpeedInGHz() ?? -1.0).ToString());
            Console.ReadLine();
        }
    }
}


I've searched for 64bit management classes, but without success. 我搜索了64位管理类,但没有成功。
Is there any other method to get the CPU speed under 64bit Windows? 还有其他方法可以在64位Windows下获得CPU速度吗?

Code below should do the trick 下面的代码应该可以解决问题

  RegistryKey registrykeyHKLM = Registry.LocalMachine;
  string keyPath = @"HARDWARE\DESCRIPTION\System\CentralProcessor\0";
  RegistryKey registrykeyCPU = registrykeyHKLM.OpenSubKey(keyPath, false);
  string MHz = registrykeyCPU.GetValue("~MHz").ToString();
  string ProcessorNameString = (string)registrykeyCPU.GetValue("ProcessorNameString");
  registrykeyCPU.Close();
  registrykeyHKLM.Close();
  Console.WriteLine("{0} MHz for {1}", MHz, ProcessorNameString);

A simpler version of the answer provided by Binoj would be as follows. Binoj提供的答案的简单版本如下。 This will return the maximum clock speed of your CPU. 这将返回CPU的最大时钟速度。 Please note that if you want the total available cycles on the machine you should multiply this value by Environment.ProcessorCount . 请注意,如果您希望机器上的总可用周期,则应将此值乘以Environment.ProcessorCount

private float GetCpuClockSpeed()
{
    return (int) Registry.GetValue(@"HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0", "~MHz", 0);
}

I've used the following code based on the answer by Binoj Antony which returns the speed for each CPU/core, not only the first one: 我根据Binoj Antony的回答使用了以下代码,该代码返回每个CPU /内核的速度,而不仅仅是第一个:

Microsoft.Win32.RegistryKey registrykeyHKLM = Microsoft.Win32.Registry.LocalMachine;
string cpuPath = @"HARDWARE\DESCRIPTION\System\CentralProcessor";
Microsoft.Win32.RegistryKey registrykeyCPUs = registrykeyHKLM.OpenSubKey(cpuPath, false);
StringBuilder sbCPUDetails = new StringBuilder();
int iCPUCount;
for (iCPUCount = 0; iCPUCount < registrykeyCPUs.SubKeyCount; iCPUCount++)
{
    Microsoft.Win32.RegistryKey registrykeyCPUDetail = registrykeyHKLM.OpenSubKey(cpuPath + "\\" + iCPUCount, false);
    string sMHz = registrykeyCPUDetail.GetValue("~MHz").ToString();
    string sProcessorNameString = registrykeyCPUDetail.GetValue("ProcessorNameString").ToString();
    sbCPUDetails.Append(Environment.NewLine + "\t" + string.Format("CPU{0}: {1} MHz for {2}", new object[] { iCPUCount, sMHz, sProcessorNameString }));
    registrykeyCPUDetail.Close();
}
registrykeyCPUs.Close();
registrykeyHKLM.Close();
sCPUSpeed = iCPUCount++ + " core(s) found:" + sbCPUDetails.ToString();

Fell free to customize it for your needs. 随意定制以满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM