python长度为n的list_为什么将列表(长度为n)传递给numba nopython函数是O(n)操作...

This is only a question to satisfy my curiosity I'm not actually planning on using lists as arguments for a numba function.

But I was wondering why passing a list to a numba function seems like an O(n) operation, while it's an O(1) operation in pure-Python functions.

Some simple example code:

import numba as nb

@nb.njit

def take_list(lst):

return None

take_list([1, 2, 3]) # warmup

And the timings:

for size in [10, 100, 1000, 10000, 100000, 1000000]:

lst = [0]*size

print(len(lst))

%timeit take_list(lst) # IPythons "magic" timeit

Results:

10

4.06 µs ± 26.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

100

14 µs ± 360 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

1000

109 µs ± 434 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

10000

1.08 ms ± 17.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

100000

10.7 ms ± 26.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

1000000

112 ms ± 383 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

解决方案

Manipulating a Python list takes Python API calls, which are forbidden in nopython mode. Numba actually copies the list contents into its own data structure, which takes time proportional to the size of the list.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值