为什么我的 python 程序没有使用完整的内核?

问题描述 投票:0回答:0

我知道除了使用诸如多处理之类的东西之外,python 只能在一个内核上运行。但是,当我使用 python 时,它使用的核心仅高达 66%,平均约为 50%。好像有个帽子当我发现它时我正在从文件中读取它,所以我将它更改为这个以确保它不是 I/O:

count = 0
while count <= 10000000000:
    count +=1

这应该是纯 python,所以没有什么可以阻止它使用完整的内核,对吧?然而现在它说一个核心是 46%,其余的大约是 1%。内存为 41%。任务栏中的电量使用情况(Windows 10)说python电量使用率很高,但cpu并没有真正动起来。我在互联网上搜索过,找不到其他类似的东西。我可以做些什么来优化这个?我运行了一个 cpuz 基准测试,它的内核达到了 100%,但是运行了一个 python 基准测试(我在 github 上找到的),一个内核再次只达到了 40% 左右。如果有帮助,这是我使用的那个:

start_benchmark = 10000 # change this if you like (sample: 1000, 5000, etc)
start_benchmark = int(start_benchmark)

repeat_benchmark = 10 # attemps, change this if you like (sample: 3, 5, etc)
repeat_benchmark = int(repeat_benchmark)

average_benchmark = 0

for a in range(0,repeat_benchmark):

  start = time.time()

  for i in range(0,start_benchmark):
    for x in range(1,1000):
      3.141592 * 2**x
    for x in range(1,10000):
      float(x) / 3.141592
    for x in range(1,10000):
      float(3.141592) / x

  end = time.time()
  duration = (end - start)
  duration = round(duration, 3)
  average_benchmark += duration
  print('Time: ' + str(duration) + 's')

average_benchmark = round(average_benchmark / repeat_benchmark, 3)
print('Average (from 10 repeats): ' + str(average_benchmark) + 's')

我还重新安装了 python 3.11,没有任何变化。任何人都知道如何让 python 发挥其全部潜力吗?如果这有助于诊断任何东西,我正在运行带有华擎 520M-HDV 的 AMD Ryzen 7 5700G。

python windows load cpu cpu-usage
© www.soinside.com 2019 - 2024. All rights reserved.