Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python code timings using numba

Tags:

python

numba

I am trying to do some timing comparisons using numba.

What I don't understand in the following mwe.py is why I get different results

from __future__ import print_function
import numpy as np
from numba import autojit
import time


def timethis(method):
    '''decorator for timing function calls'''
    def timed(*args, **kwargs):
        ts = time.time()
        result = method(*args, **kwargs)
        te = time.time()
        print('{!r} {:f} s'.format(method.__name__, te - ts))
        return result
    return timed


def pairwise_pure(x):
    '''sample function, compute pairwise distancee, see: jakevdp.github.io/blog/2013/06/15/numba-vs-cython-take-2/'''
    M, N = x.shape
    D = np.empty((M, M), dtype=np.float)
    for i in range(M):
        for j in range(M):
            d = 0.
            for k in range(N):
                tmp = x[i, k] - x[j, k]
                d += tmp * tmp
            D[i, j] = np.sqrt(d)
    return D

# first version
@timethis
@autojit
def pairwise_numba(args):
    return pairwise_pure(args)

# second version
@timethis
def pairwise_numba_alt(args):
    return autojit(pairwise_pure)(args)

x = np.random.random((1e3, 10))

pairwise_numba(x)
pairwise_numba_alt(x)

Evaluating python3 mwe.py gives this output:

'pairwise_numba' 5.971631 s
'pairwise_numba_alt' 0.191500 s

In the first version, I decorate the method using timethis to calculate the timings, and with autojit to speed up the code , whereas in the second one I decorate the function with timethis, and call autojit(...) afterwards.

Does someone have an explanation ?

like image 860
t-bltg Avatar asked Jul 17 '26 12:07

t-bltg


1 Answers

Actually the documentation explicitly states that for optimization each call to other functions "inside" a decorated function should be decorated as well or it isn't optimized.

For many functions like numpy functions that isn't necessary since they are highly optimized but for native python functions it is.

like image 55
MSeifert Avatar answered Jul 20 '26 01:07

MSeifert