小编ken*_*hfm的帖子

python while 循环的高 CPU 使用率:即使在 97% 的时间处于睡眠状态。为什么?

我试图找出为什么以下非常简单、最小的示例在我的 i7-5500U CPU、Windows 10 计算机上占用了大约 33% 的 CPU 使用率:

import time
import numpy as np
import scipy.linalg
import cProfile

class CPUTest:
    def __init__(self):
        self.running = True

    def compute_stuff(self):
        dims = 150
        A = np.random.random((dims, dims))
        B = scipy.linalg.inv(np.dot(A.T, A))

    def run(self):
        prev_time = time.time()
        start_time = prev_time
        while self.running:
            time.sleep(0.3)
            st = time.time()
            self.compute_stuff()
            et = time.time()
            print 'Time for the whole iteration, inc. sleep: %.3f (ms), whereas the processing segment took %.3f (ms): ' % ((st - prev_time) * …
Run Code Online (Sandbox Code Playgroud)

python cpu sleep while-loop

5
推荐指数
2
解决办法
5334
查看次数

标签 统计

cpu ×1

python ×1

sleep ×1

while-loop ×1