Comparison of memory occupied by dict and set

As we all know, the query efficiency of set is slightly better than that of dict and far better than list.

However, what is the specific comparison between the memory space occupied by set and list?

We wrote this piece of code for comparison:

import sys

a={
    
    }
b=set([])

for i in range(1000000):
    print i
    a[i]=1
    b.add(i)

print 'space of dict',sys.getsizeof(a)
print 'space of set',sys.getsizeof(b)

The final memory usage is compared as follows:

space of dict 50331920
space of set 33554656

The memory footprint of set is about one-third less than that of dict.

Guess you like

Origin blog.csdn.net/esa72ya/article/details/102021683