Как и выше, почему функция cpu_usage показывает 100% процессорного времени, тогда как в HWMonitor или в Windows Диспетчер задач я вижу только 10-11% использования. Я получил код, который измеряет использование процессора и оперативной памяти. Использование памяти, кажется, работает как шарм, но этот процессор как-то в 10 раз больше, чем в диспетчере задач. Почему?
import random
import threading
import psutil
def display_cpu():
global running
running = True
currentProcess = psutil.Process()
# start loop
while running:
print("CPU: ",currentProcess.cpu_percent(interval=1), "%", "| Memory: ", currentProcess.memory_info().rss/(1024*1024), "MB")
def start():
global t
# create thread and start it
t = threading.Thread(target=display_cpu)
t.start()
def stop():
global running
global t
# use `running` to stop loop in thread so thread will end
running = False
# wait for thread's end
t.join()
# ---
def insertion_sort():
nums = []
for i in range(30000):
nums.append(random.randint(1, 10000))
for i in range(1, len(nums)):
item_to_insert = nums[i]
j = i - 1
while j >= 0 and nums[j] > item_to_insert:
nums[j + 1] = nums[j]
j -= 1
nums[j + 1] = item_to_insert
# ---
for i in range(1):
start()
try:
result = insertion_sort()
finally:
stop()