怎么用脚本获取处理器频率

wen 实用脚本 2

本文目录导读:

怎么用脚本获取处理器频率

  1. Linux 系统
  2. Windows 系统
  3. macOS 系统
  4. 跨平台 Python 方案
  5. 注意事项

获取处理器频率的方法取决于你的操作系统,以下是常用的脚本方法:

Linux 系统

使用 bash 脚本

#!/bin/bash
# 获取所有CPU核心的当前频率
echo "当前CPU频率:"
cat /proc/cpuinfo | grep "MHz" | head -n 1 | awk '{print $4}'
# 获取最大/最小频率
echo "最大频率:"
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
echo "最小频率:"
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
# 使用 lscpu 命令
lscpu | grep "MHz"

使用 Python

import os
def get_cpu_freq():
    # 方法1: 解析 /proc/cpuinfo
    with open('/proc/cpuinfo', 'r') as f:
        for line in f:
            if 'MHz' in line:
                return float(line.split(':')[1].strip())
    # 方法2: 读取 sysfs 文件系统
    try:
        with open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq', 'r') as f:
            freq_khz = int(f.read().strip())
            return freq_khz / 1000  # 转换为 MHz
    except:
        return None
print(f"CPU频率: {get_cpu_freq()} MHz")

Windows 系统

使用 PowerShell

# 方法1: 使用 WMI
Get-WmiObject -Class Win32_Processor | Select-Object Name, MaxClockSpeed, CurrentClockSpeed
# 方法2: 更详细的输出
Get-CimInstance -ClassName Win32_Processor | 
    Select-Object Name, @{Name="Speed(MHz)";Expression={$_.MaxClockSpeed}}
# 方法3: 实时获取当前频率
$cpu = Get-WmiObject -Class Win32_Processor
$cpu.CurrentClockSpeed

使用 VBScript

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Processor")
For Each objItem in colItems
    Wscript.Echo "处理器名称: " & objItem.Name
    Wscript.Echo "最大频率: " & objItem.MaxClockSpeed & " MHz"
    Wscript.Echo "当前频率: " & objItem.CurrentClockSpeed & " MHz"
Next

macOS 系统

使用 bash

#!/bin/bash
# 方法1: 使用 sysctl
sysctl -n machdep.cpu.brand_string
sysctl -n hw.cpufrequency
# 方法2: 使用 system_profiler
system_profiler SPHardwareDataType | grep "Processor Speed"
# 方法3: 获取当前频率(需要额外工具)
# 可以使用第三方工具如 cpufreq

使用 Python

import subprocess
def get_cpu_freq_mac():
    # 使用 sysctl 命令
    result = subprocess.run(['sysctl', '-n', 'machdep.cpu.brand_string'], 
                          capture_output=True, text=True)
    return result.stdout.strip()
print(f"CPU信息: {get_cpu_freq_mac()}")

跨平台 Python 方案

import platform
import subprocess
def get_cpu_freq():
    system = platform.system().lower()
    if system == 'linux':
        try:
            with open('/proc/cpuinfo', 'r') as f:
                for line in f:
                    if 'MHz' in line:
                        return f"{float(line.split(':')[1].strip()):.2f} MHz"
        except:
            pass
    elif system == 'windows':
        try:
            result = subprocess.run(['wmic', 'cpu', 'get', 'CurrentClockSpeed'], 
                                  capture_output=True, text=True)
            lines = result.stdout.strip().split('\n')
            if len(lines) > 1:
                return f"{lines[1].strip()} MHz"
        except:
            pass
    elif system == 'darwin':  # macOS
        try:
            result = subprocess.run(['sysctl', '-n', 'machdep.cpu.brand_string'], 
                                  capture_output=True, text=True)
            return result.stdout.strip()
        except:
            pass
    return "无法获取CPU频率"
print(f"CPU频率: {get_cpu_freq()}")

注意事项

  • Windows 上的 CurrentClockSpeed 可能显示为 0,这是正常现象
  • Linux 上需要确保 cpufreq 驱动已加载才能获取实时频率
  • macOS 对硬件信息的访问限制较多,可能需要管理员权限
  • 某些系统可能需要安装额外工具(如 cpufrequtils

选择适合你操作系统的脚本进行测试即可。

抱歉,评论功能暂时关闭!