开发者

In Python why does a tuple of integers take up less space than distinct integers?

开发者 https://www.devze.com 2023-03-07 12:26 出处:网络
Here\'s an example with random integers: a, b, c, d = 79412623, 56529819571, 10431, 30461 t 开发者_如何学Go= (79412623, 56529819571, 10431, 30461)

Here's an example with random integers:

a, b, c, d = 79412623, 56529819571, 10431, 30461
t 开发者_如何学Go= (79412623, 56529819571, 10431, 30461)

And their sizes:

import sys
sys.getsizeof(t) # 88
aa, bb, cc, dd = sys.getsizeof(a), sys.getsizeof(b), sys.getsizeof(c), sys.getsizeof(d)
sum([aa,bb,cc,dd]) # 96

Why does the tuple take up less space?


The number returned by sys.getsizeof doesn't include the size of the objects contained by a container.

>>> sys.getsizeof({1:2})
280
>>> sys.getsizeof({'a_really_long_string_that_takes_up_lots_of_space':'foo'})
280


I'm working on a 32-bit Windows XP, with Python 2.6.2, and I tried your code, which looks like this:

In [15]: a,b,c,d=79412623, 56529819571, 10431, 30461

In [16]: t=(79412623, 56529819571, 10431, 30461)

In [17]: sys.getsizeof(t) Out[17]: 44

In [18]: aa, bb, cc, dd = sys.getsizeof(a), sys.getsizeof(b), sys.getsizeof(c), sys.getsizeof(d)

In [19]: sum([aa,bb,cc,dd]) Out[19]: 56

0

精彩评论

暂无评论...
验证码 换一张
取 消