0

Not sure what is going on here or if I am correctly using Numba, but the speed here is not coming in as desired; please let me know what I am missing to get numba to work the way it should.

Outputs show runfile('#####) : Numba-> 0.14654635999977472 :Python-> 0.00047117299982346594

import timeit from numpy import log, exp from numba import jit

@jit(nopython=True)
def Func():
    n=100
    b=[]
    for i  in range(0,n):
        b.append(log((i/2+52)**2)*exp(.05*10))
   
    
    return(i)

print(timeit.timeit(Func,number=100))



def Func2():
    n=100
    c=[]
    for j  in range(0,n):
        c.append(log((j/2+52)**2)*exp(.05*10))
   
    
    return(j)

print(timeit.timeit(Func,number=100))
1
  • Side note: you need not redefine Func2: you can use Func.py_func to access the uncompiled python function. Commented Dec 2, 2020 at 17:34

1 Answer 1

1

Two reasons for speed discrepancy: (1) Numba has to compile your code before running it. Try calling function once outside of timer (force compile), then do timing

(2) you are only doing 100 iterations, which is nothing for a computer. The noise from OS activity, granularity of timer, etc will dwarf speed difference on such a small sample. Try doing 10,000 iterations or more (after addressing compile issue)

EDIT — if the OP increased his loop to 1mm iterations (see his comment below), then the code is spending almost all of its time resizing arrays, not doing any calculations.

Embedded math functions (eg exp and log) are already compiled, there is nothing for numba to work with. Numba compilation can only speed up calculations and loops that aren’t already compiled. As written, loops cannot be unwound or executed in parallel because append() creates a serial dependency.

If the OP wants to see the differences between numba and plain python, he should look for code examples with lots of manual calculations (not embedded math functions). Calculating the mandlebrot set would be an example; numba will do those calculations many times faster than stand alone python.

Sign up to request clarification or add additional context in comments.

2 Comments

(1) Code was compiled and ran multiple times to produce the numbers above. I am aware of the need to run it once before measuring.
(2) Raised the N to a million; same story. What you have posted is not the reason, must be something else. Very curious to know what it is ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.