小编Chr*_*her的帖子

调用numba jit函数时,cProfile会增加很多开销

将纯Python无操作函数与装饰的无操作函数进行比较@numba.jit,即:

import numba

@numba.njit
def boring_numba():
    pass

def call_numba(x):
    for t in range(x):
        boring_numba()

def boring_normal():
    pass

def call_normal(x):
    for t in range(x):
        boring_normal()
Run Code Online (Sandbox Code Playgroud)

如果我们计算时间%timeit,我们会得到以下结果:

%timeit call_numba(int(1e7))
792 ms ± 5.51 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

%timeit call_normal(int(1e7))
737 ms ± 2.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Run Code Online (Sandbox Code Playgroud)

一切都很合理; numba函数的开销很小,但并不多.

但是,如果我们使用cProfile这个代码进行分析,我们会得到以下结果:

cProfile.run('call_numba(int(1e7)); call_normal(int(1e7))', sort='cumulative')

   ncalls  tottime  percall  cumtime  percall …
Run Code Online (Sandbox Code Playgroud)

python performance profiling cprofile numba

13
推荐指数
1
解决办法
522
查看次数

标签 统计

cprofile ×1

numba ×1

performance ×1

profiling ×1

python ×1