如何在python中使用带有mpmath / gmpy的JIT?

问题描述 投票:3回答:1

这是我第一次尝试使用JIT for python,这是我想要加速的用例。我读了一些关于numba的内容,看起来很简单,但下面的代码没有提供任何加速。请原谅我可能犯的任何明显错误。

我也尝试过做cython的基本教程,但是时间上没有区别。 http://docs.cython.org/src/tutorial/cython_tutorial.html

我猜我必须做一些像声明变量的事情?使用其他图书馆?专门用于所有东西的循环?我很感激我可以参考的任何指导或示例。

例如,我从之前的问题Elementwise operations in mpmath slow compared to numpy and its solution知道使用gmpy而不是mpmath明显更快。

import numpy as np
from scipy.special import eval_genlaguerre
from sympy import mpmath as mp
from sympy.mpmath import laguerre as genlag2
import collections

from numba import jit

import time

def len2(x):
    return len(x) if isinstance(x, collections.Sized) else 1

@jit # <-- removing this doesn't change the output time if anything it's slower with this
def laguerre(a, b, x):
    fun = np.vectorize(genlag2)
    return fun(a, b, x)

def f1( a, b, c ):

    t       = time.time()
    M       = np.ones( [ len2(a), len2(b), len2(c) ] )
    A, B, C = np.meshgrid( a, b, c, indexing = 'ij' )
    temp    = laguerre(A, B, C)
    M      *= temp
    print 'part1:      ', time.time() - t
    t       = time.time()

    A, B    = np.meshgrid( a, b, indexing= 'ij' )
    temp    = np.array( [[ mp.fac(x1)/mp.fac(y1) for x1,y1 in zip(x2,y2)] for x2,y2 in zip(A, B)] )
    temp    = np.reshape( temp, [ len(a), len(b), 1 ] )
    temp    = np.repeat(  temp, len(c), axis = 2 )
    print 'part2 so far:', time.time() - t
    M      *= temp
    print 'part2 finally', time.time() - t
    t       = time.time()

a = mp.arange( 30 )
b = mp.arange( 10 )
c = mp.linspace( 0, 100, 100 )

M = f1( a, b, c)
python cython jit numba mpmath
1个回答
1
投票

最好使用带有自定义装饰器的vectorize的numba,如果没有定义,则会执行延迟操作,这可能会导致进程变慢。在我看来,与矢量化相比,Jit很慢。

© www.soinside.com 2019 - 2024. All rights reserved.