python获取cpu当前速度

Python018

python获取cpu当前速度,第1张

你好,下面是windows的获取cpu频率的代码。

def get_windows_cpu_speed():

import winreg

key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"HARDWARE\DESCRIPTION\System\CentralProcessor\0")

speed, type = winreg.QueryValueEx(key, "~MHz")

speed = round(float(speed)/1024, 1)

return "{speed} GHz".format(speed=speed)

用psutil包

cpu:

>>> import psutil

>>> psutil.cpu_times()

scputimes(user=3961.46, nice=169.729, system=2150.659, idle=16900.540, iowait=629.59, irq=0.0, softirq=19.42, steal=0.0, guest=0, nice=0.0)

>>>

>>> for x in range(3):

...     psutil.cpu_percent(interval=1)

...

4.0

5.9

3.8

>>>

>>> for x in range(3):

...     psutil.cpu_percent(interval=1, percpu=True)

...

[4.0, 6.9, 3.7, 9.2]

[7.0, 8.5, 2.4, 2.1]

[1.2, 9.0, 9.9, 7.2]

>>>

>>>

>>> for x in range(3):

...     psutil.cpu_times_percent(interval=1, percpu=False)

...

scputimes(user=1.5, nice=0.0, system=0.5, idle=96.5, iowait=1.5, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)

scputimes(user=1.0, nice=0.0, system=0.0, idle=99.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)

scputimes(user=2.0, nice=0.0, system=0.0, idle=98.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)

>>>

>>> psutil.cpu_count()

4

>>> psutil.cpu_count(logical=False)

2

>>>

内存

>>> psutil.virtual_memory()

svmem(total=8374149120L, available=2081050624L, percent=75.1, used=8074080256L, free=300068864L, active=3294920704, inactive=1361616896, buffers=529895424L, cached=1251086336)

>>> psutil.swap_memory()

sswap(total=2097147904L, used=296128512L, free=1801019392L, percent=14.1, sin=304193536, sout=677842944)

>>>