# -*- 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}))
<class 'NoneType'> size is 16 Byte <class 'float'> size is 24 Byte <class 'int'> size is 28 Byte <class 'bool'> size is 28 Byte <class 'str'> size is 49 Byte 51 <class 'tuple'> size is 56 Byte 64 <class 'list'> size is 72 Byte 80 <class 'set'> size is 232 Byte 232 <class 'dict'> size is 248 Byte 248
32位 int:開銷= 10字節,值= 4字節 float:開銷= 8字節,value = 8字節 64位 int:開銷= 20字節,值= 8字節 float:開銷= 16個字節,值= 8個字節
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