我试图准确/明确地找到Python中两个不同类之间的大小差异.它们都是新的样式类,除了没有定义槽的一个.我已经尝试了很多测试来确定它们的大小差异,但它们总是在内存使用方面完全相同.
到目前为止,我已经尝试了sys.GetSizeOf(obj)和heapy的heap()函数,没有任何积极的结果.测试代码如下:
import sys
from guppy import hpy
class test3(object):
def __init__(self):
self.one = 1
self.two = "two variable"
class test4(object):
__slots__ = ('one', 'two')
def __init__(self):
self.one = 1
self.two = "two variable"
test3_obj = test3()
print "Sizeof test3_obj", sys.getsizeof(test3_obj)
test4_obj = test4()
print "Sizeof test4_obj", sys.getsizeof(test4_obj)
arr_test3 = []
arr_test4 = []
for i in range(3000):
arr_test3.append(test3())
arr_test4.append(test4())
h = hpy()
print h.heap()
Run Code Online (Sandbox Code Playgroud)
输出:
Run Code Online (Sandbox Code Playgroud)Sizeof test3_obj 32 Sizeof test4_obj 32 Partition of a set of 34717 …