Is it possible that numba jit slows down my gcd execution?

Viewed 151

I'm trying to compute a large amount of GCD(x,y) as part of Euler 625 and since this takes a lot of time, I tried adding @numba jit to speed it up.

I checked similar question like this, this and this but they are not helpful (I'm not using numpy or using a parallel-able functions (like sum) and I called the functions several times - so compiling laziness isn't the issue either) The results are :

with    numba: 8.220806121826172 seconds
without numba: 1.242861270904541 seconds

my code is added below.

  1. why is numba slowing me down?
  2. how can I execute this faster (the original limit is 10**11, which is incomputable in this way as it seems)

    import time
    from numba import jit, int32
    
    gcd_dict = {}
    
    #@jit
    def gcd(x, y ,k = None, reverse_k = None):
    
        if not k:
            k = (x,y)
            reverse_k = (y,x)
    
        if k in gcd_dict:
            return gcd_dict[k]
        elif reverse_k in gcd_dict:
            return gcd_dict[reverse_k]
    
        if not y:
            if len(gcd_dict) < 10**6:
                gcd_dict[k] = x
                gcd_dict[reverse_k] = x
            return x
        else:
            return gcd(y, x % y, k, reverse_k)
    
    
    
    def main():
        t = time.time()
        s = 0
    #    i_limit = 10**11+1
        i_limit = 10**3
    
        for i in range(1,i_limit):
            for j in range(1,i+1):
                s += gcd(i,j)
    
        print(s)
        print(time.time() - t)
    
    main()
    
1 Answers

Dictionaries are not supported by numba, thus it falls back to Python mode which causes runtime overhead.

You can see that JIT compilation fails if you replace @jit by @jit(nopython=True) which disables switching to Python so that you get an error message in case you use unsupported Python features.

Related