Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

python - getsizeof returns the same value for seemingly different lists

I have the following two dimensional bitmap:

num = 521
arr = [i == '1' for i in bin(num)[2:].zfill(n*n)]
board = [arr[n*i:n*i+n] for i in xrange(n)]

Just for curiosity I wanted to check how much more space will it take, if it will have integers instead of booleans. So I checked the current size with sys.getsizeof(board) and got 104

After that I modified

arr = [int(i) for i in bin(num)[2:].zfill(n*n)] , but still got 104

Then I decided to see how much will I get with just strings:

arr = [i for i in bin(num)[2:].zfill(n*n)], which still shows 104

This looks strange, because I expected list of lists of strings to waste way more memory than just booleans.

Apparently I am missing something about how the getsizeof calculates the size. Can anyone explain me why I get such results.

P.S. thanks to zehnpard's answer, I see that I can use sum(sys.getsizeof(i) for line in board for i in line) to approximately count the memory (most probably it will not count the lists, which is not that much important for me). Now I see the difference in numbers for string and int/bool (no difference for int and boolean)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The docs for the sys module since Python 3.4 is pretty explicit:

Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.

Given that Python lists are effectively arrays of pointers to other Python objects, the number of elements a Python list contains will influence its size in memory (more pointers) but the type of objects contained will not (memory-wise, they aren't contained in the list, just pointed at).

To get the size of all items in a container, you need a recursive solution, and the docs helpfully provide a link to an activestate recipe. http://code.activestate.com/recipes/577504/

Given that this recipe is for Python 2.x, I'm sure this behavior was always standard, and got explicitly mentioned in the docs since 3.4 onwards.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...