python - getsizeof returns the same value for seemingly different lists -
i have following 2 dimensional bitmap:
num = 521 arr = [i == '1' in bin(num)[2:].zfill(n*n)] board = [arr[n*i:n*i+n] in xrange(n)]
just curiosity wanted check how more space take, if have integers instead of booleans. checked current size sys.getsizeof(board)
, got 104
after modified
arr = [int(i) in bin(num)[2:].zfill(n*n)]
, still got 104
then decided see how strings:
arr = [i in bin(num)[2:].zfill(n*n)]
, still shows 104
this looks strange, because expected list of lists of strings waste way more memory booleans.
apparently missing how getsizeof calculates size. can explain me why such results.
p.s. zehnpard's answer, see can use sum(sys.getsizeof(i) line in board in line)
approximately count memory (most not count lists, not important me). see difference in numbers string , int/bool (no difference int , boolean)
the docs sys module since python 3.4 pretty explicit:
only memory consumption directly attributed object accounted for, not memory consumption of objects refers to.
given python lists arrays of pointers other python objects, number of elements python list contains influence size in memory (more pointers) type of objects contained not (memory-wise, aren't contained in list, pointed at).
to size of items in container, need recursive solution, , docs helpfully provide link activestate recipe. http://code.activestate.com/recipes/577504/
given recipe python 2.x, i'm sure behavior standard, , got explicitly mentioned in docs since 3.4 onwards.
Comments
Post a Comment