作者:倩女甜言蜜语_182 | 来源:互联网 | 2023-05-18 13:11
基本类型占用的内存类型#-*-coding:utf-8-*-#@time:2019-12-1911:16#@author:bingerimportsysanoneb1000.231
基本类型占用的内存
- 类型
# -*- coding: utf-8 -*-
# @time : 2019-12-19 11:16
# @author : binger
import sys
a = none
b = 1000.2311
c = 1000
d = true
e = ""
f = ()
g = []
h = set([])
i = {}
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)), sys.getsizeof("12"))
print(" %s size is %d " % (type(f), sys.getsizeof(f)), sys.getsizeof((1,)))
print(" %s size is %d " % (type(g), sys.getsizeof(g)), sys.getsizeof([1, ]))
print(" %s size is %d " % (type(h), sys.getsizeof(h)), sys.getsizeof(set([1, ])))
print(" %s size is %d " % (type(i), sys.getsizeof(i)), sys.getsizeof({1: 1}))
- 结果:
内存大小排行: none
size is 16 byte
size is 24 byte
size is 28 byte
size is 28 byte
size is 49 byte 51
size is 56 byte 64
size is 72 byte 80
size is 232 byte 232
size is 248 byte 248
分析:
- 在64位 python 2 中 int 和 float 均为 24b。但是 int 不包含 long类型(28b)
from bintrees import bintree
import uuid, time, sys
import random
def create_uuid(msg):
src_uuid = uuid.uuid4()
name = "{}{}".format(time.time(), msg)
return uuid.uuid3(src_uuid, name=name).hex
a = {create_uuid(i): random.randint(0, 10) for i in range(2000)}
b = {i: i for i in range(2000)}
ring = bintree.binarytree()
c = [ring.insert(create_uuid(i), i) for i in range(2000)]
ring2 = bintree.binarytree()
d = [ring2.insert(i, i) for i in range(2000)]
print("字典1", sys.getsizeof(a))
print("字典2", sys.getsizeof(b))
print("二叉树:", sys.getsizeof(c))
print("二叉树:", sys.getsizeof(d))
字典1 73832
字典2 73832
二叉树: 16568
二叉树: 16568