Issue
I have two identical functions, say, sum_nb
and sum_nb2
. I define them with @njit
decorator:
from numba import njit
from timeit import timeit
@njit
def sum_nb(n=100_000_000):
s = 0
for i in range(n):
s += i
return s
@njit
def sum_nb2(n=100_000_000):
s = 0
for i in range(n):
s += i
return s
if I just save as script and add code to measure execution time, everything behaves nicely:
print(sum_nb())
print(sum_nb2())
print(timeit(sum_nb))
print(timeit(sum_nb2))
The output is:
4999999950000000
4999999950000000
0.41249959499691613
0.4120563520118594
Now I open ipython console/jupyter lab and copy the first code to the cell. Then I measure code time in cells with magic:
In [3]: %timeit sum_nb()
240 ns ± 86.5 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [4]: %timeit sum_nb2()
7.32 µs ± 90 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
The same happens if in jupyter lab. How does it happen? How it works? Why does the same code have different speed?
I have ipython 7.7.0, numba 0.44.1, python 3.7.3, jupyter lab 1.0.2
Solution
This appears to be the result of inconsistent caching behavior. Jupyter even suggests this as an issue: The slowest run took 74.96 times longer than the fastest. This could mean that an intermediate result is being cached.
On my machine, the function with the caching behavior runs in ~240ns and the one without runs in ~50μs.
The only way I've found to make the behavior consistent between the two functions is by pulling n=100_000_000
into the body of the function which makes both functions ~240ns.
Answered By - Gus
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.