Numba @jit(nopython = True)函数无法在较重的Numpy函数上提高速度

时间:2019-02-14 20:29:44

标签: python performance numpy numba

我当前正在运行test_matrix_speed(),以查看我的search_and_book_availability函数有多快。使用PyCharm分析器,我可以看到每个search_and_book_availability函数调用的平均速度为0.001ms。使用Numba @jit(nopython=True)装饰器对该函数的性能没有影响。这是因为没有可改进的地方,并且Numpy在这里运行得尽可能快吗? (我不在乎generate_searches函数的速度)

这是我正在运行的代码

import random

import numpy as np
from numba import jit


def generate_searches(number, sim_start, sim_end):
    searches = []
    for i in range(number):
        start_slot = random.randint(sim_start, sim_end - 1)
        end_slot = random.randint(start_slot + 1, sim_end)
        searches.append((start_slot, end_slot))
    return searches


@jit(nopython=True)
def search_and_book_availability(matrix, search_start, search_end):
    search_slice = matrix[:, search_start:search_end]
    output = np.where(np.sum(search_slice, axis=1) == 0)[0]
    number_of_bookable_vecs = output.size
    if number_of_bookable_vecs > 0:
        if number_of_bookable_vecs == 1:
            id_to_book = output[0]
        else:
            id_to_book = np.random.choice(output)
        matrix[id_to_book, search_start:search_end] = 1
        return True
    else:
        return False


def test_matrix_speed():
    shape = (10, 1440)
    matrix = np.zeros(shape)
    sim_start = 0
    sim_end = 1440
    searches = generate_searches(1000000, sim_start, sim_end)
    for i in searches:
        search_start = i[0]
        search_end = i[1]
        availability = search_and_book_availability(matrix, search_start, search_end)

1 个答案:

答案 0 :(得分:1)

使用您的函数和以下代码来分析速度

import time

shape = (10, 1440)
matrix = np.zeros(shape)
sim_start = 0
sim_end = 1440
searches = generate_searches(1000000, sim_start, sim_end)

def reset():
    matrix[:] = 0

def test_matrix_speed():
    for i in searches:
        search_start = i[0]
        search_end = i[1]
        availability = search_and_book_availability(matrix, search_start, search_end)

def timeit(func):
    # warmup
    reset()
    func()

    reset()
    start = time.time()
    func()
    end = time.time()

    return end - start

print(timeit(test_matrix_speed))

我发现jit的版本大约是11.5s,而没有jit的版本大约是7.5s。我不是numba的专家,但是它的目的是优化以非向量化方式(尤其是显式for循环)编写的数字代码。在您的代码中没有,您仅使用向量化操作。因此,我希望jit不会跑赢基准解决方案,尽管我必须承认令我感到惊讶的是,情况如此糟糕。如果您想优化解决方案,可以使用以下代码减少执行时间(至少在我的PC上):

def search_and_book_availability_opt(matrix, search_start, search_end):
    search_slice = matrix[:, search_start:search_end]

    # we don't need to sum in order to check if all elements are 0.
    # ndarray.any() can use short-circuiting and is therefore faster.
    # Also, we don't need the selected values from np.where, only the
    # indexes, so np.nonzero is faster
    bookable, = np.nonzero(~search_slice.any(axis=1))

    # short circuit
    if bookable.size == 0:
        return False

    # we can perform random choice even if size is 1
    id_to_book = np.random.choice(bookable)
    matrix[id_to_book, search_start:search_end] = 1
    return True

,然后将matrix初始化为np.zeros(shape, dtype=np.bool),而不是默认的float64。我能够执行大约3.8s的执行时间,比您未绑定的解决方案节省了约50%,比您的未绑定解决方案节省了约70%。希望有帮助。