6

This is my first attempt to use JIT for python and this is the use case I want to speed up. I read a bit about numba and it seemed simple enough but the following code didn't provide any speedup. Please excuse any obvious mistakes I may be making.

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

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

For example I know from a previous question Elementwise operations in mpmath slow 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'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)
Community
  • 1
  • 1
evan54
  • 3,585
  • 5
  • 34
  • 61

1 Answers1

2

Better to use numba with vectorize with self-defined decorators, if not defined lazy action will execute, which may lead to slow down the process. Jit is slow compared to vectorize in my opinion.

  • Welcome to Stack Overflow! While this might be a valuable hint to solve the problem, a good answer also demonstrates the solution. Please [edit] to provide example code to show what you mean. Alternatively, consider writing this as a comment instead. – Toby Speight Feb 28 '18 at 10:23