Reserve memory for list in Python?

前端 未结 7 1012
北恋
北恋 2020-12-15 02:33

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

7条回答
  •  隐瞒了意图╮
    2020-12-15 03:16

    In Python, all objects are allocated on the heap.
    But Python uses a special memory allocator so malloc won't be called every time you need a new object.
    There are also some optimizations for small integers (and the like) which are cached; however, which types, and how, is implementation dependent.

提交回复
热议问题