位置: 编程技术 - 正文
推荐整理分享浅谈Python 对象内存占用(python的对象),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:python对象包括哪些,python对象的方法,python对象的三个要素,python对象的方法,python对象的属性和方法,python对象的三个要素,python3对象,python对象包括哪两大类,内容如对您有帮助,希望把文章链接给更多的朋友!
一切皆是对象
在 Python 一切皆是对象,包括所有类型的常量与变量,整型,布尔型,甚至函数。 参见stackoverflow上的一个问题 Is everything an object in python like ruby
代码中即可以验证:
# everythin in python is object def fuction(): return print isinstance(True, object) print isinstance(0, object) print isinstance('a', object) print isinstance(fuction, object)
如何计算
Python 在 sys 模块中提供函数 getsizeof 来计算 Python 对象的大小。
当然,对象内存占用与 Python 版本以及操作系统版本关系密切, 本文的代码和测试结果都是基于 windows7 位操作系统。
import sys print sys.version
2.7.2 (default, Jun , ::) [MSC v. bit (Intel)]
基本类型
布尔型
print 'size of True: %d' % (sys.getsizeof(True)) print 'size of False: %d' % (sys.getsizeof(False))
输出:
size of True: size of False:
整型
# normal integer print 'size of integer: %d' % (sys.getsizeof(1)) # long print 'size of long integer: %d' % (sys.getsizeof(1L)) print 'size of big long integer: %d' % (sys.getsizeof(L)) 输出:
size of integer: x size of long integer 1L: size of long integer L:
可以看出整型占用字节,长整型最少占用字节,且占用空间会随着位数的增多而变大。 在2.x版本,如果整型类型的值超出sys.maxint,则自动会扩展为长整型。而 Python 3.0 之后,整型和长整型统一为一种类型。
浮点型
print 'size of float: %d' % (sys.getsizeof(1.0))
输出:
size of float:
浮点型占用个字节。超过一定精度后会四舍五入。
参考如下代码:
print 1. print 1.
输出:
1. 1.
字符串
# size of string type print 'rn'.join(["size of string with %d chars: %d" % (len(elem), sys.getsizeof(elem)) for elem in ["", "a", "ab"]]) # size of unicode string print 'rn'.join(["size of unicode string with %d chars: %d" % (len(elem), sys.getsizeof(elem)) for elem in [u"", u"a", u"ab"]])
输出:
size of string with 0 chars: size of string with 1 chars: size of string with 2 chars: size of unicode string with 0 chars: size of unicode string with 1 chars: size of unicode string with 2 chars:
普通空字符串占个字节,每增加一个字符,多占用1个字节。Unicode字符串最少占用个字节,每增加一个字符,多占用2个字节。
集合类型
列表
# size of list type print 'rn'.join(["size of list with %d elements: %d" % (len(elem), sys.getsizeof(elem)) for elem in [[], [0], [0,2], [0,1,2]]])
输出:
size of list with 0 elements: size of list with 1 elements: size of list with 2 elements: size of list with 3 elements:
可见列表最少占用个字节,每增加一个元素,增加4个字节。但要注意,sys.getsizeof 函数并不计算容器类型的元素大小。比如:
print 'size of list with 3 integers %d' % (sys.getsizeof([0,1,2])) print 'size of list with 3 strings %d' % (sys.getsizeof(['0','1','2']))
输出:
size of list with 3 integers size of list with 3 strings
容器中保存的应该是对元素的引用。如果要准确计算容器,可以参考recursive sizeof recipe 。使用其给出的 total_size 函数:
print 'total size of list with 3 integers %d' % (total_size([0,1,2])) print 'total size of list with 3 strings %d' % (total_size(['0','1','2']))
输出为:
total size of list with 3 integers total size of list with 3 strings
可以看出列表的空间占用为 基本空间 + (对象引用 4 + 对象大小) * 元素个数。
另外还需注意如果声明一个列表变量,则其会预先分配一些空间,以便添加元素时增加效率:
li = [] for i in range(0, ): print 'list with %d integers size: %d, total_size: %d' % (i, getsizeof(li), total_size(li)) li.append(i)
元组
基本与列表类似,但其最少占用为个字节。
字典
字典的情况相对复杂很多,具体当然要参考代码 dictobject.c, 另外 NOTES ON OPTIMIZING DICTIONARIES 非常值得仔细阅读。
基本情况可以参考[stackoverflow] 的问题 Python's underlying hash data structure for dictionaries 中的一些回答:
字典最小拥有8个条目的空间(PyDict_MINSIZE);条目数小于,时,每次增长4倍;条目数大于,时,每次增长2倍;键的hash值缓存在字典中,字典调整大小后不会重新计算;
每接近2/3时,字典会调整大小。
以上这篇浅谈Python 对象内存占用就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持积木网。
python 性能提升的几种方法 关于python性能提升的一些方案。一、函数调用优化(空间跨度,避免访问内存)程序的优化核心点在于尽量减少操作跨度,包括代码执行时间上的跨度
python 实现网上商城,转账,存取款等功能的信用卡系统 一、要求二、思路1.购物类buy接收信用卡类的信用卡可用可用余额,返回消费金额2.信用卡(ATM)类接收上次操作后,信用卡可用余额,总欠款,剩余欠
Python在线运行代码助手 Python代码运行助手可以让你在线输入Python代码,然后通过本机运行的一个Python脚本来执行代码。原理如下:在网页输入代码:点击Run按钮,代码被发送
标签: python的对象
本文链接地址:https://www.jiuchutong.com/biancheng/386210.html 转载请保留说明!友情链接: 武汉网站建设