Numba autojit function slower than vectorized Numpy method

wigging

I have the following for-loop to construct a list of values:

p = 7
A = []

for i in range(0, 10**p):
    A.append(i**3 + i**2)

To speed up the creation of the list, I created it as a Numpy array using a vectorized approach. This approach is much faster than the equivalent for-loop, especially for large values of p which increases the range.

import numpy as np
from numba import autojit

p = 7
m = np.arange(0, 10**p)
D = np.empty(len(m))
D = m**3 + m**2

To speed up the creation of the array even further, I thought I would try to use the Numba package. Below is my current attempt.

@autojit
def func(a):
    a = np.asarray(a)
    n = np.arange(0, 10**p)
    a = np.append(a, n**3 + n**2)
    return a

e = []
E = func(e)

Unfortunately I am not seeing any performance gains from using Numba which is almost 3x slower than the vectorized approach using just Numpy.

Any suggestions on how to use Numba for this?

JoshAdel

Numba doesn't make arbitrary method calls faster. If you are calling out to a library, numba really can't do anything with that most of the time. But if you re-write things a little differently, you can still get a decent speedup (I'm using numba 0.14.0 -- if you are using a different version, hardware, etc, you might get different results, especially since numba is in active development):

import numpy as np
import numba as nb

def func(a, p):
    a = np.asarray(a)
    n = np.arange(0, 10**p)
    a = np.append(a, n**3 + n**2)
    return a

@nb.jit
def func2(a, p):
    a = np.asarray(a)
    n = np.empty(10**p, dtype=np.float64)
    for k in range(10**p):
        n[k] = k*k*(k + 1)

    return np.append(a, n)

p = 6
e = []
E = func(e, p)
E2 = func2(e, p)
print np.allclose(E, E2)

And timings:

In [51]:

%timeit func(e, p)
10 loops, best of 3: 42.9 ms per loop
In [52]:

%timeit func2(e, p)
100 loops, best of 3: 3.09 ms per loop

Also with p=7 you need to be a little careful about numerical precision.

The key with numba is to unroll loops and only make "primitive" arithmetic calls that numba supports

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is Cython slower than vectorized NumPy?

From Dev

Why is Cython slower than vectorized NumPy?

From Dev

Numba 3x slower than numpy

From Dev

Multiply function slower in Numba than CPython

From Dev

AttributeError: 'str' object has no attribute '_ptr' in Numba @autojit function

From Dev

Numpy "vectorized" row-wise dot product running slower than a for loop

From Dev

Vectorized code slower than for loop in Matlab

From Dev

Vectorized code slower than loops? MATLAB

From Dev

Vectorized code slower than for loop in Matlab

From Dev

Why is Cython so much slower than Numba when iterating over NumPy arrays?

From Dev

Numba slower than pure Python in frequency counting

From Dev

Numba code slower than pure python

From Dev

Numba slower than pure Python in frequency counting

From Dev

Numba function slower than C++ and loop re-order further slows down x10

From Dev

numba slower for numpy.bitwise_and on boolean arrays

From Dev

What is the difference between jit and autojit in numba?

From Dev

Returned dtype of numpy vectorized function

From Dev

Simulate numpy vectorized function on a meshgrid

From Dev

numba guvectorize target='parallel' slower than target='cpu'

From Dev

numpy faster than numba and cython , how to improve numba code

From Dev

Theano GPU calculation slower than numpy

From Dev

Extract is slower than fancy index in numpy?

From Dev

Adding arrays with cython slower than numpy?

From Dev

Numpy individual element access slower than for lists

From Dev

why is dot product in dask slower than in numpy

From Dev

xtensor's "operator/" slower than numpy's "/"

From Dev

Theano GPU calculation slower than numpy

From Dev

Julia matrix multiplication is slower than numpy's

From Dev

ruby performance - is &: slower than just calling method?

Related Related

  1. 1

    Why is Cython slower than vectorized NumPy?

  2. 2

    Why is Cython slower than vectorized NumPy?

  3. 3

    Numba 3x slower than numpy

  4. 4

    Multiply function slower in Numba than CPython

  5. 5

    AttributeError: 'str' object has no attribute '_ptr' in Numba @autojit function

  6. 6

    Numpy "vectorized" row-wise dot product running slower than a for loop

  7. 7

    Vectorized code slower than for loop in Matlab

  8. 8

    Vectorized code slower than loops? MATLAB

  9. 9

    Vectorized code slower than for loop in Matlab

  10. 10

    Why is Cython so much slower than Numba when iterating over NumPy arrays?

  11. 11

    Numba slower than pure Python in frequency counting

  12. 12

    Numba code slower than pure python

  13. 13

    Numba slower than pure Python in frequency counting

  14. 14

    Numba function slower than C++ and loop re-order further slows down x10

  15. 15

    numba slower for numpy.bitwise_and on boolean arrays

  16. 16

    What is the difference between jit and autojit in numba?

  17. 17

    Returned dtype of numpy vectorized function

  18. 18

    Simulate numpy vectorized function on a meshgrid

  19. 19

    numba guvectorize target='parallel' slower than target='cpu'

  20. 20

    numpy faster than numba and cython , how to improve numba code

  21. 21

    Theano GPU calculation slower than numpy

  22. 22

    Extract is slower than fancy index in numpy?

  23. 23

    Adding arrays with cython slower than numpy?

  24. 24

    Numpy individual element access slower than for lists

  25. 25

    why is dot product in dask slower than in numpy

  26. 26

    xtensor's "operator/" slower than numpy's "/"

  27. 27

    Theano GPU calculation slower than numpy

  28. 28

    Julia matrix multiplication is slower than numpy's

  29. 29

    ruby performance - is &: slower than just calling method?

HotTag

Archive