7

list众所周知,它会使用一大块空间进行初始化,以优化扩展列表所需的时间(平均而言,我们不必像数组一样继续创建新列表)。

怎么样set

以下构造使其空间浪费,因为list. 我知道tuple更节省空间,因为它是不可变的。我们可以对它做同样的事情set并且仍然是可变的吗?

set( [ 1, 2, 3] )

4

2 回答 2

11
>>> from sys import getsizeof as size
>>> s = set(xrange(100))
>>> l = list(xrange(100))
>>> size(s)
8424
>>> size(l)
1016

sets 比 s 占用更多的内存list。s 提供的某些功能set需要更多内存(例如快速成员资格测试)。

于 2012-11-25T03:04:05.073 回答
4

似乎对于小列表,集合可以占用 10 倍的内存,但在较大的列表中,由于某种原因,这会下降到大约 3 倍。(可能是由于哈希表冲突导致链表链接,这会减慢查找速度,但有助于内存使用?)

在 python3 中,getsizeof(range()) 总是返回一个常量,因为范围对象有点像迭代器,所以我尝试通过制作实际列表来测试它。编辑:我可以尝试使用 getsizeof(list(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
于 2019-02-26T17:44:12.220 回答