How to use JIT efficiently in python with mpmath / gmpy?

This is my first attempt at JIT for python and this is the use case I want to speed up. I read a little about numba and it seemed simple enough, but the following code did not provide any acceleration. Please excuse any obvious mistakes I may make.

I also tried to do what the main cython tutorial suggests, but again no time difference. http://docs.cython.org/src/tutorial/cython_tutorial.html

I'm guessing I need to do something like declare variables? Use other libraries? Use for loops exclusively for everything? I would appreciate any guidance or examples I can refer to.

For example, I know from a previous question Element operations in mpmath are slower compared to numpy and its solution that using gmpy instead of mpmath was significantly faster.

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 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)

      

+3


source to share


1 answer


It is better to use numba with vectorization with self-defined decorators, if not defined, a lazy action will take place, which can slow down the process. Jit is slow compared to vectorization in my opinion.



+1


source







All Articles