[英]Why does completely same function behave differently in ipython/jupyter?
I have two identical functions, say, sum_nb
and sum_nb2
.我有两个相同的函数,比如sum_nb
和sum_nb2
。 I define them with @njit
decorator:我用@njit
装饰器定义它们:
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: output 是:
4999999950000000
4999999950000000
0.41249959499691613
0.4120563520118594
Now I open ipython console/jupyter lab and copy the first code to the cell.现在我打开 ipython 控制台/jupyter 实验室并将第一个代码复制到单元格。 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.如果在 jupyter 实验室中也会发生同样的情况。 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我有 ipython 7.7.0、numba 0.44.1、python 3.7.3、jupyter lab 1.0.2
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.
Jupyter 甚至认为这是一个问题: The slowest run took 74.96 times longer than the fastest. This could mean that an intermediate result is being cached.
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.在我的机器上,具有缓存行为的 function 运行时间约为 240ns,而没有缓存行为的运行时间约为 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.我发现使两个函数之间的行为保持一致的唯一方法是将n=100_000_000
拉入 function 的主体中,这使得两个函数都约为 240ns。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.