为什么将长度为n的列表传递给numba nopython函数是O(n)操作 [英] Why is passing a list (of length n) to a numba nopython function an O(n) operation

查看:135
本文介绍了为什么将长度为n的列表传递给numba nopython函数是O(n)操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是一个满足我好奇心的问题,我实际上并没有计划将列表用作numba函数的参数.

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

但是我想知道为什么将列表传递给numba函数似乎是O(n)操作,而在纯Python函数中却是O(1)操作.

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.

一些简单的示例代码:

import numba as nb

@nb.njit
def take_list(lst):
    return None

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

时间:

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

    lst = [0]*size
    print(len(lst))
    %timeit take_list(lst)   # IPythons "magic" timeit

结果:

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)

推荐答案

处理Python列表需要使用Python API调用,在nopython模式下禁止使用. Numba实际上将列表内容复制到其自己的数据结构中,这花费的时间与列表的大小成正比.

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.

这篇关于为什么将长度为n的列表传递给numba nopython函数是O(n)操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆