K
K
knn77132020-09-01 21:34:48
Python
knn7713, 2020-09-01 21:34:48

Why is code with numba slower than code without it?

There is a class, I create many of its instances, for each of which it is necessary to perform certain mathematical calculations. To make this whole thing work faster, I'm trying to use numba.

@jitclass([('ind', uint)])
class CheckTheory:
    def __init__(self, ind):
        self.ind = ind
    def complex_math_calculations(self, inc_value):
        self.ind += inc_value

@njit(parallel=True, fastmath=True)
def main_func():       
    classSamplesArray = []
    # init class samples
    for i in range(10000):
        classSamplesArray.append(CheckTheory(i))
    for sample in classSamplesArray:
        sample.complex_math_calculations(10)

start_time = time.time()
main_func()
print(time.time() - start_time)


BUT: execution time with numba: 0.1412217617034912 seconds
without numba: 0.018378019332885742 seconds

What am I doing wrong? How can you speed up the execution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
StarCatSTT, 2020-09-02
@knn7713

Try doing this instead of the last three lines:

start_time = time.time()
for x in range(1000):
    main_func()
print(time.time() - start_time)

I got these results:

Numba:  1.2769
No numba: 6.77


The first execution is the longest since numba "compiles" the code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question