在 Python 中为列表保留内存? [英] Reserve memory for list in Python?

查看:24
本文介绍了在 Python 中为列表保留内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Python 编程时,是否可以为将填充已知数量的项目的列表保留内存,以便在构建列表时不会多次重新分配列表?我已经查看了 Python 列表类型的文档,但没有找到任何似乎可以做到这一点的内容.但是,这种类型的列表构建会出现在我代码的几个热点中,因此我希望使其尽可能高效.

When programming in Python, is it possible to reserve memory for a list that will be populated with a known number of items, so that the list will not be reallocated several times while building it? I've looked through the docs for a Python list type, and have not found anything that seems to do this. However, this type of list building shows up in a few hotspots of my code, so I want to make it as efficient as possible.

另外,用 Python 这样的语言做这样的事情是否有意义?我是一个相当有经验的程序员,但对 Python 还是个新手,并且仍然对它的做事方式有所了解.Python 是否在内部将所有对象分配在单独的堆空间中,从而违背了尝试最小化分配的目的,或者像整数、浮点数等原语直接存储在列表中?

Also, does it even make sense to do something like this in a language like Python? I'm a fairly experienced programmer, but new to Python and still getting a feel for its way of doing things. Does Python internally allocate all objects in separate heap spaces, defeating the purpose of trying to minimize allocations, or are primitives like ints, floats, etc. stored directly in lists?

推荐答案

这里有四个变体:

  • 增量列表创建
  • 预分配"列表
  • array.array()
  • numpy.zeros()

 

python -mtimeit -s"N=10**6" "a = []; app = a.append;"\
    "for i in xrange(N):  app(i);"
10 loops, best of 3: 390 msec per loop

python -mtimeit -s"N=10**6" "a = [None]*N; app = a.append;"\
    "for i in xrange(N):  a[i] = i"
10 loops, best of 3: 245 msec per loop

python -mtimeit -s"from array import array; N=10**6" "a = array('i', [0]*N)"\
    "for i in xrange(N):" "  a[i] = i"
10 loops, best of 3: 541 msec per loop

python -mtimeit -s"from numpy import zeros; N=10**6" "a = zeros(N,dtype='i')"\
    "for i in xrange(N):" "  a[i] = i"
10 loops, best of 3: 353 msec per loop

它表明在这种情况下 [None]*N 是最快的,array.array 是最慢的.

It shows that [None]*N is the fastest and array.array is the slowest in this case.

这篇关于在 Python 中为列表保留内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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