Let's create a lists-of-lists:
l = []
for i in range(1000000):
l.append(['abcdefghijklmnopqrstuvwxyz', '1.8', '5', 'john@email.com', 'ffffffffff'])
l.__sizeof__()
reports 8697440
and process occupies 111 MB
.
Now let's try list-of-tuples instead of lists-of-lists:
l = []
for i in range(1000000):
l.append(('abcdefghijklmnopqrstuvwxyz', '1.8', '5', 'john@email.com', 'ffffffffff'))
l.__sizeof__()
reports 8697440
and process occupies 12 MB
.
As expected, huge improvement. Now let's cast the list into tuples just before insertion instead:
l = []
for i in range(1000000):
l.append(tuple(['abcdefghijklmnopqrstuvwxyz', '1.8', '5', 'john@email.com', 'ffffffffff']))
l.__sizeof__()
reports 8697440
and process occupies 97 MB
.
Why 97 MB, and not 12 MB ??
Is it garbage? gc.collect()
reports 0
.
Doing a del l
releases all the memory down to 4 MB
, which is just a little over what a blank interpreter occupies. So, l
was actually bloated.
When we cast a list into tuple, is the resulting tuple "impure" compared to tuple created from scratch ?
And if so, is there a workaround to convert list into "pure" tuple?