Python基本数据类型内存占用详解与实测

沿途有光
沿途有光
发布时间:2026-10-13 | 3人浏览过
原创

本文通过Python代码实例,详细展示了int、bool、long、float、str、list、tuple、dict及set等基本数据类型的内存占用情况。利用sys.getsizeof方法获取具体字节数,揭示Python与其他语言在内存管理上的差异,为开发者优化程序性能提供参考。

在Python中,基本数据类型所占据的内存空间大小与其他编程语言存在显著差异。

import sys
a = 100
b = True
c = 100L
d = 1.1
e =""
f = []
g =()
h = {}
i = set([])

print " %s size is %d "%(type(a),sys.getsizeof(a))
print " %s size is %d "%(type(b),sys.getsizeof(b))
print " %s size is %d "%(type(c),sys.getsizeof(c))
print " %s size is %d "%(type(d),sys.getsizeof(d))
print " %s size is %d "%(type(e),sys.getsizeof(e))
print " %s size is %d "%(type(f),sys.getsizeof(f))
print " %s size is %d "%(type(g),sys.getsizeof(g))
print " %s size is %d "%(type(h),sys.getsizeof(h))
print " %s size is %d "%(type(i),sys.getsizeof(i))

 <type 'int'> size is 12 
 <type 'bool'> size is 12 
 <type 'long'> size is 14 
 <type 'float'> size is 16 
 <type 'str'> size is 21 
 <type 'list'> size is 36 
 <type 'tuple'> size is 28 
 <type 'dict'> size is 140 
 <type 'set'> size is 116 

以上关于Python基本数据类型内存占用大小的实例分享完毕,希望能为读者提供有价值的参考。欢迎继续支持本站,获取更多技术内容。

相关标签:

本站声明

本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。

Python 相关产品和应用

全部下载

专题列表