pympler asizeof与sys.getsizeof [英] pympler asizeof vs sys.getsizeof

查看:180
本文介绍了pympler asizeof与sys.getsizeof的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有个腌菜.它的大小是9.3MB.

I have a pickled filed. Its size is 9.3MB.

-rw-r--r-- 1 ankit ankit 9.3M Jan  7 17:43 agg_397127.pkl

我使用cPickle将其加载到python中.我试图使用pympler asizeof确定其大小.但是,和sys.getsizeof

I load it in python using cPickle. I tried to ascertain its size using pympler asizeof. But there is a considerable difference size given by asize of and sys.getsizeof

from pympler import asizeof
import cPickle as pickle
path = "agg_397127.pkl"
temp  = pickle.load(open(path, 'rb'))
temp
{397127: RandomForestRegressor(bootstrap=True, criterion='band_predict',
           max_depth=None, max_features='auto', max_leaf_nodes=None,
           min_samples_leaf=1, min_samples_split=2,
           min_weight_fraction_leaf=0.0, n_estimators=1000, n_jobs=1,
           oob_score=False, random_state=0, verbose=0, warm_start=False)}
asizeof.asizeof(temp)
1328504
asizeof.flatsize(temp)
import sys
sys.getsizeof(temp)
280

有人可以解释为什么有这样的区别吗?

Can someone explain why there is such a difference ?

推荐答案

sys.getsizeof()返回传递给它的对象的大小-在您的示例中,这是一本只有一个条目的字典.它不包括字典所引用的复杂类实例的大小,也不包含该实例所引用的任何对象的大小.仅有少量条目(在我的Python版本中最多为5个)的任何词典都将返回完全相同的数字.

sys.getsizeof() returns the size of the object passed to it - which is a dictionary with one entry, in your example. It does NOT include the size of the complex class instance referred to by the dictionary, nor any of the objects referred to by that instance. ANY dictionary with only a few entries (up to 5, on my Python version) would return exactly the same number.

您正在使用的assizeof模块尝试递归加总所有这些引用对象的大小.考虑到返回的大小与泡菜大小之间的巨大差异,在这种情况下,它似乎做得并不是很好(但是请注意,这些数字永远不会完全相等,因为磁盘上的泡菜的格式必定是与内存中实际对象的格式不同).

The assizeof module you're using attempts to recursively add up the sizes of all these referred objects. It doesn't seem to have done a very good job in this case, considering the huge discrepancy between the size returned and the pickle size (but note that these numbers would never be exactly equal, since the format of a pickle on disk is necessarily different than the format of the actual objects in memory).

这篇关于pympler asizeof与sys.getsizeof的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆