Not seeing much of a speedup

0 views
Skip to first unread message

bkj...@gmail.com

unread,
May 19, 2017, 3:11:40 PM5/19/17
to Numba Public Discussion - Public
Hi everyone --

I'm fairly new to Numba, and was wondering whether someone could explain why the performance of this code doesn't seem to benefit very much at all from the JIT.  Any suggestions on how to speed this up would be much appreciated.  Superficially, this seems to me like the kind of thing that Numba should be good at -- but maybe I'm wrong.

Thanks
Ben

```
import numpy as np
from numba import autojit

def ub_cases(a, b, mode):
    if (a > 0) and (b > 0):
        return (a - b) ** 2
    elif (a > 0) and (b < 0):
        return a ** 2 if mode == 'l' else -b * (a ** 2)
    elif (a < 0) and (b > 0):
        return b ** 2 if mode == 't' else -a * (b ** 2)
    else:
        return 0

def awarp(s, t):
    D = np.zeros((s.shape[0] + 1, t.shape[0] + 1)).astype('int')
    D[:,0] = int(1e10)
    D[0,:] = int(1e10)
    D[0,0] = 0
    
    for i in range(s.shape[0]):
        for j in range(t.shape[0]):
            if (i > 0) and (j > 0):
                a_d = D[i,j] + ub_cases(s[i], t[j], mode='d')
            else:
                a_d = D[i,j] + (s[i] - t[j]) ** 2
            
            a_t = D[i+1,j] + ub_cases(s[i], t[j], mode='t')
            a_l = D[i,j+1] + ub_cases(s[i], t[j], mode='l')
            
            D[i+1,j+1] = np.min([a_d, a_t, a_l])
    
    return D[-1,-1]


awarp_ = autojit(awarp)

s = np.random.choice((-3, 1), 100)
t = np.random.choice((-3, 1), 100)
%timeit awarp(s, t)
%timeit awarp_(s, t)
```

Kevin Sheppard

unread,
May 19, 2017, 3:24:36 PM5/19/17
to Numba Public Discussion - Public
You need to jit `ub_cases` since it is triggering nopython mode.


--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users...@continuum.io.
To post to this group, send email to numba...@continuum.io.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/numba-users/aa79cb67-140b-466b-88c4-4d2265fecbc3%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Leopold Haimberger

unread,
May 19, 2017, 3:26:00 PM5/19/17
to Numba Public Discussion - Public
Yeah, that should work if you use an integer for the mode keyword
Leo
Reply all
Reply to author
Forward
0 new messages