python 基本数据类型占用内存空间大小的实例

yipeiwu_com6年前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设计】。

相关文章

numpy实现合并多维矩阵、list的扩展方法

一、合并多个numpy矩阵 1、首先创建两个多维矩阵 矩阵a的大小为(2,3,2) 矩阵b的大小为(3,2,3) 采用concatentate这个函数就可以合并两个多维矩阵 合并之后...

Python中的ConfigParser模块使用详解

1.基本的读取配置文件     -read(filename) 直接读取ini文件内容     -sections() 得到所...

2款Python内存检测工具介绍和使用方法

去年自己写过一个程序时,不太确定自己的内存使用量,就想找写工具来打印程序或函数的内存使用量。这里将上次找到的2个内存检测工具的基本用法记录一下,今后分析Python程序内存使用量时也是需...

浅谈python新手中常见的疑惑及解答

1 lambda函数 函数格式是lambda keys:express   匿名函数lambda是一个表达式函数,接受keys参数,返回表达式的值。所以不用retur...

浅谈Python 集合(set)类型的操作——并交差

阅读目录 •介绍 •基本操作 •函数操作 介绍 python的set是一个无序不重复元素集,基本功能包括关系测试和消除重复元素. 集合对象还支持并、交...