我该如何让python程序使用更多的CPU,因为它仅使用40%?

时间:2019-04-24 14:17:11

标签: python multithreading multiprocessing

我想在程序中添加多重处理。我将如何去做?

import random
import matplotlib.pyplot as plt

Numbers = []
Xs = []
Ys = []
num_Dices = 50
dice_Rolls = 1000000


def fill_y():
    global Numbers
    Numbers = sorted(Numbers, key=int)
    current_num = num_Dices
    for i in range(0, (num_Dices * 6 - (num_Dices - 1))):
        count = 0
        for j in range(0, dice_Rolls):
            if Numbers[j] == current_num:
                count += 1
        current_num += 1
        Ys.append(count)


def fill_x():
    i = num_Dices
    while i < num_Dices * 6 + 1:
        Xs.append(i)
        i += 1


def roll(n, w):
    while w != 0:
        r = 0
        total_sum = 0
        if w != 0:
            while r < n:
                a = random.randint(1, 6)
                total_sum += a
                r += 1
        Numbers.append(total_sum)
        w += -1


roll(num_Dices, dice_Rolls)
fill_x()
fill_y()
plt.plot(Xs, Ys)
plt.xlabel("Total from rolls")
plt.ylabel("Frequency")
plt.show()

我希望我的程序使用更多的cpu,以便可以更快地得出结论。它仅使用40%左右,任何帮助都会很棒

0 个答案:

没有答案