Повышение производительности скрипта python с помощью numba jit - PullRequest
2 голосов
/ 08 апреля 2020

Я запускаю выборочное моделирование python, чтобы предсказать взвешенные и регулярные кости. Я хотел бы использовать numba, чтобы ускорить мой сценарий, но я получаю сообщение об ошибке:

<timed exec>:6: NumbaWarning: 
Compilation is falling back to object mode WITH looplifting enabled because Function "roll" failed type inference due to: Untyped global name 'sum': cannot determine Numba type of <class 'builtin_function_or_method'>

File "<timed exec>", line 9:
<source missing, REPL/exec in use?>

Вот мой оригинальный код: есть ли другой тип выражения numba, который я могу использовать вместо этого? Сейчас я тестирую, используя ввод 2500 рулонов; Я хочу уменьшить это до 4 секунд (сейчас это 8,5 секунд).

%%time
from numba import jit
import random
import matplotlib.pyplot as plt
import numpy

@jit
def roll(sides, bias_list):
    assert len(bias_list) == sides, "Enter correct number of dice sides"
    number = random.uniform(0, sum(bias_list))
    current = 0
    for i, bias in enumerate(bias_list):
        current += bias
        if number <= current:
            return i + 1

no_of_rolls = 2500
weighted_die = {}
normal_die = {}
#weighted die

for i in range(no_of_rolls):
        weighted_die[i+1]=roll(6,(0.15, 0.15, 0.15, 0.15, 0.15, 0.25))
#regular die  
for i in range(no_of_rolls):
        normal_die[i+1]=roll(6,(0.167, 0.167, 0.167, 0.167, 0.167, 0.165))

plt.bar(*zip(*weighted_die.items()))
plt.show()
plt.bar(*zip(*normal_die.items()))
plt.show()

Ответы [ 2 ]

2 голосов
/ 09 апреля 2020

Использование случайного выбора

Рефакторированный код

import random
import matplotlib.pyplot as plt

no_of_rolls = 2500

# weights
normal_weights = (0.167, 0.167, 0.167, 0.167, 0.167, 0.165)
bias_weights = (0.15, 0.15, 0.15, 0.15, 0.15, 0.25)

# Replaced roll function with random.choices 
# Reference: https://www.w3schools.com/python/ref_random_choices.asp
bias_rolls = random.choices(range(1, 7), weights = bias_weights, k = no_of_rolls)
normal_rolls = random.choices(range(1, 7), weights = normal_weights, k = no_of_rolls)

# Create dictionaries with same structure as posted code
weighted_die = dict(zip(range(no_of_rolls), bias_rolls))
normal_die = dict(zip(range(no_of_rolls), normal_rolls))

# Use posted plotting calls
plt.bar(*zip(*weighted_die.items()))
plt.show()
plt.bar(*zip(*normal_die.items()))
plt.show()

Производительность

*Not including plotting.*
Original code: ~6 ms
Revised code:  ~2 ms
(3x improvement, but not sure why the post mentions 8 seconds to run)
0 голосов
/ 09 апреля 2020

Вы можете ускорить его, используя guvectorize

%%time
from numba import guvectorize
import matplotlib.pyplot as plt
import numpy as np
import random

sides = 6
bias_list = (0.15, 0.15, 0.15, 0.15, 0.15, 0.25)

@guvectorize(["f8[:,:], uint8[:]"], "(n, k) -> (n)", nopython=True)
def roll(biases, side):
    for i in range(biases.shape[0]):
        number = random.uniform(0, np.sum(biases[i,:]))
        current = 0
        for j, bias in enumerate(biases[i,:]):
            current += bias
            if number <= current:
                side[i] = j + 1
                break

no_of_rolls = 2500
biases = np.zeros((no_of_rolls,len(bias_list)))

biases[:,] = np.array(bias_list)

normal_die = roll(biases)

print(normal_die)

Это заняло ~ 200 мс на моем P C, в то время как Ваш код около 6 se c.

...