list
初始化时占用了大量空间来优化扩展列表所需的时间(平均而言,我们不必像列阵那样制作新的列表)。
set
怎么样?
以下结构因list
而浪费空间。我理解tuple
更节省空间,因为它是不可变的。我们可以对set
做同样的事情并且仍然可变吗?
set( [ 1, 2, 3] )
答案 0 :(得分:9)
>>> from sys import getsizeof as size
>>> s = set(xrange(100))
>>> l = list(xrange(100))
>>> size(s)
8424
>>> size(l)
1016
set
占用更多内存而非list
s。 set
提供的一些功能需要更多内存(例如快速成员资格测试)。
答案 1 :(得分:0)
对于小列表,似乎集可以占用10倍的内存,但是在大列表中,由于某种原因,它可以减少到大约3倍。 (也许是由于哈希表冲突导致链表链链接,这会减慢查找速度,但有助于内存使用?)
在python3中,由于range对象有点像迭代器,因此getsizeof(range())始终返回一个常量,因此我尝试通过制作实际列表进行测试。
from sys import getsizeof # returns memory size in bytes
list_lengths = [100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000] # from 100 to 1billion
for length in list_lengths:
my_list = [num for num in range(length)]
my_set = set(my_list)
print("list length: {}".format(len(my_list)))
print("memory size of my_list: {}".format(getsizeof(my_list)))
print("memory size of my_set: {}".format(getsizeof(my_set)))
print("set to list size ratio: {}".format(getsizeof(my_set)/getsizeof(my_list)))
Output:
list length: 100
memory size of my_list: 912
memory size of my_set: 8416
set to list size ratio: 9.228070175438596
list length: 1000
memory size of my_list: 9024
memory size of my_set: 32992
set to list size ratio: 3.6560283687943262
list length: 10000
memory size of my_list: 87624
memory size of my_set: 524512
set to list size ratio: 5.985939925134666
list length: 100000
memory size of my_list: 824464
memory size of my_set: 4194528
set to list size ratio: 5.087581750082478
list length: 1000000
memory size of my_list: 8697464
memory size of my_set: 33554656
set to list size ratio: 3.8579815909557085
list length: 10000000
memory size of my_list: 81528056
memory size of my_set: 268435680
set to list size ratio: 3.2925558779421897
list length: 100000000
memory size of my_list: 859724472
memory size of my_set: 4294967520
set to list size ratio: 4.995748824048828
list length: 1000000000
memory size of my_list: 8058558880
memory size of my_set: 34359738592 # my computer doesn't have this much memory, not sure what's going on here. Maybe it's writing to SSD
set to list size ratio: 4.263757218089594