简体   繁体   English

为什么完全相同的 function 在 ipython/jupyter 中的行为不同?

[英]Why does completely same function behave differently in ipython/jupyter?

I have two identical functions, say, sum_nb and sum_nb2 .我有两个相同的函数,比如sum_nbsum_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.

相关问题 为什么PyQt4在Jupyter和IPython Notebook之间的行为有所不同? - Why does PyQt4 behave differently between Jupyter and IPython notebook? 为什么 Python 对相同的函数(例如“sum”或“and”)表现不同? - Why does Python behave differently for the same function such as 'sum' or 'and'? 当类在函数中时,为什么类中的全局行为会有所不同? - why does global in a class behave differently when the class is within a function? 为什么 for 循环的行为与“pop” function 的 while 循环不同? - Why does the for loop behave differently than the while loop for the “pop” function? 为什么 Popen() function 在 Python 和 PyGTK 中的行为不同? - Why does the Popen() function behave differently in Python and PyGTK? 从Jupyter笔记本运行时,为什么龙卷风AsyncHTTPClient的行为会有所不同? - Why does tornado AsyncHTTPClient behave differently when run from a jupyter notebook? 在没有打印功能的情况下显示数据时,IPython和REPL的行为会有所不同 - IPython and REPL behave differently when displaying data without the print function python jupyter 在 if 语句中测试的相同条件表现不同 - python jupyter Same condition tested in an if statement behave differently 为什么这个上下文管理器与dict理解有不同的表现? - Why does this contextmanager behave differently with dict comprehensions? 为什么 numpy import 的行为不同? - Why does numpy import behave differently?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM