Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I compile numba in the setup phase of a unit test?

I used numba to accelerate parts of my Python code using the autojit decorator. Tests pass and %timeit shows that the code is indeed accelerated.

The first execution is slow because, I assume, numba is compiling the code. Makes sense. But when I run a suite of tests, they run extremely slowly, from 10 tests in about 10 seconds before my changes to the same tests in 117 seconds. It seems like numba must be compiling again and again, separately for each test.

Can I avoid this? I have tried running one simple test in the setUp function, thinking that might compile the function there once for all the tests, but this did not change the run time significantly.

like image 413
Dan Allan Avatar asked Dec 31 '25 03:12

Dan Allan


1 Answers

Something like this might work:

from numba import autojit

def autojit_except_when_unit_testing(func):
    if UNIT_TESTING:
        return func
    return autojit(func)

Possibly also bump numba's issues regarding caching, as this is a pretty important use case. I would normally be pretty hesitant to run unit tests and production code in such different environments, but unit tests that take forever don't get run as often. You should almost certainly test with numba as well, just less often.

like image 73
U2EF1 Avatar answered Jan 02 '26 15:01

U2EF1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!