假设我以两种方式创建python lists
。
在第一种情况下,我使用简单的赋值:
my_list = []
print(my_list, '->', my_list.__sizeof__())
my_list = [1]
print(my_list, '->', my_list.__sizeof__())
my_list = [1, 1]
print(my_list, '->', my_list.__sizeof__())
在第二种情况下,我在列表中使用append()
方法:
my_list = []
print(my_list, '->', my_list.__sizeof__())
my_list.append(1)
print(my_list, '->', my_list.__sizeof__())
my_list.append(1)
print(my_list, '->', my_list.__sizeof__())
但是我得到了意想不到的(对我来说)输出:
=== WITH ASSIGNMENT ===
([], '->', 40)
([1], '->', 48)
([1, 1], '->', 56)
=== WITH APPEND ===
([], '->', 40)
([1], '->', 72)
([1, 1], '->', 72)
Python内存管理内部会发生什么?为什么'相同'列表有不同的大小?
答案 0 :(得分:14)
当您附加到列表时,由于性能原因,内存被过度分配到列表中,因此多个附加不需要对列表进行相应的内存重新分配,这会降低列表中的整体性能重复追加的情况。
CPython源清楚地在comment中描述了这种行为:
/* This over-allocates proportional to the list size, making room
* for additional growth. The over-allocation is mild, but is
* enough to give linear-time amortized behavior over a long
* sequence of appends() in the presence of a poorly-performing
* system realloc().
* The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
* Note: new_allocated won't overflow because the largest possible value
* is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
*/
在另一种情况下,列表是从文字构造的,列表的大小反映了容器本身的大小以及对每个包含对象的引用。
确切的分配行为可能因其他Python实现而异(请参阅Jython和PyPy的list.append
实现),并且不保证存在过度分配。