如何编译numba python代码并存储它?

2024-04-19 23:55:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在使用numba来加速一些for循环,得到了相当好的结果。我如何预编译代码,而不是让代码及时编译(这需要一些时间)?在

下面是一个例子:

import numba as nb
import numpy as np
import time.time()

Nk = 5
Nl = 6
Nx = 7
Ny = 8
A = np.random.rand(Nk, Nl, Nx, Ny)

@nb.jit(nopython=True)
def Loop( A, X, Y ):
    Nk = A.shape[0]
    Nl = A.shape[1]
    Nx = A.shape[2]
    Ny = A.shape[3]
    for ik in range(Nk):
        for il in range(Nl):
            for ix in range(Nx):
                for iy in range(Ny):
                    Y[ik, il] += A[ik, il, ix, iy]*X[ix,iy]
    return Y

Y = np.zeros([Nk, Nl])
X = np.random.rand(Nx, Ny)
Y = Loop( A, X , Y )

我想要的是以某种方式保存编译后的函数,这样我就不需要每次编译它了。在


Tags: inimportfornlnprangeilik
1条回答
网友
1楼 · 发布于 2024-04-19 23:55:56

原则上你有pycc,但从今天起(numba 0.17)the API is not stable

The API for ahead of time compilation isn’t stabilized. Adventurous users can still try the pycc utility which is installed as part of Numba.

然而,熟练的读者可以提取一些信息from the source itself。在

相关问题 更多 >