Significant slow down on njit'd functions when optional arguments not provided
See original GitHub issueHello I ran into a problem where an njit’d function that has optional arguments is almost 200 times slower then when the arguments are provided.
I saw a similar issue from a few years ago which was resolved. Not sure if this is related or not. My numba and python versions are below.
import sys
import numba
import numpy as np
@numba.njit
def calc(x_squared, order_l: int = 2, init_l: int = 10):
z = x_squared / (2. * init_l + 3.)
for l_fake in range(order_l - 1, init_l - 1):
l = init_l - l_fake
l += 1
z = x_squared / ((2. * l + 1.) - z)
return z
%timeit calc(0.1)
%timeit calc(0.1, order_l=2, init_l=10)
# Perhaps an initial compile issue (even though timeit should catch that.), try running again
print('2nd Run')
%timeit calc(0.1)
%timeit calc(0.1, order_l=2, init_l=10)
print('\n')
print('python version:', sys.version)
print('numba version:', numba.__version__)
print('numpy version:', np.__version__)
Running this leads to the following output
54.3 µs ± 554 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
332 ns ± 3.34 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
2nd Run
54 µs ± 258 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
331 ns ± 2.76 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
python version: 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)]
numba version: 0.54.1
numpy version: 1.20.3
As you can see the non argument provided version runs in around 54 us versus the provided version which is only 0.3 us!
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Optionally passing parameters onto another function with jit
I have modified the code from the solution provided in the github issue to suit your example: from numba import jitclass, int32, njit...
Read more >Using Python Optional Arguments When Defining Functions
In this tutorial, you'll explore the techniques you have available for defining Python functions that take optional arguments. When you master Python ...
Read more >How to test if optional argument has been given?
I understand that the last call ( f[1] ) returns True because arg does not exist and thus SameQ tests a single argument...
Read more >Performance Tips — Numba 0.50.1 documentation
No Python mode vs Object mode¶. A common pattern is to decorate functions with @jit as this is the most flexible decorator offered...
Read more >Performance difference between optional args and keyword ...
I think what you are seeing is no significant difference. In my machine, the keyword version is slightly slower:
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Correct, this is definitely a performance bug, although a long standing one, apparently.
The problem seems to have existed for as far back as numba 0.35.0. That’s the earliest accessible version in the current version of conda.