相关疑难解决方法(0)

在Python中准确测量对象大小 - Sys.GetSizeOf无法正常工作

我试图准确/明确地找到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)

输出:

Sizeof test3_obj 32
Sizeof test4_obj 32

Partition of a set of 34717 …
Run Code Online (Sandbox Code Playgroud)

python memory slots

8
推荐指数
2
解决办法
4157
查看次数

标签 统计

memory ×1

python ×1

slots ×1