import array
import json
from sys import getsizeof
bi = 1 << 100 * 10000
bij = json.dumps(bi)
print('int', getsizeof(bij), len(bij))
bs = json.dumps(bin(bi))
print('str', getsizeof(bs), len(bs))
bx = json.dumps(hex(bi))
print('hex', getsizeof(bx), len(bx))
a = array.array('u', bx)
ai = a.tobytes()
print('array', getsizeof(ai), len(ai))
输出
int 301079 301030
str 1000054 1000005
hex 250054 250005
array 500043 500010
本文通过Python代码展示了不同数据类型(整型、字符串和十六进制表示)在内存中所占空间的对比,并使用了array模块进一步研究数组类型的内存消耗。
6005

被折叠的 条评论
为什么被折叠?



