栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

是否有适用于python2.7的内存分析器?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

是否有适用于python2.7的内存分析器?

您可能想要尝试将以下代码适应您的特定情况并支持您的数据类型:

import sysdef sizeof(variable):    def _sizeof(obj, memo):        address = id(obj)        if address in memo: return 0        memo.add(address)        total = sys.getsizeof(obj)        if obj is None: pass        elif isinstance(obj, (int, float, complex)): pass        elif isinstance(obj, (list, tuple, range)): if isinstance(obj, (list, tuple)):     total += sum(_sizeof(item, memo) for item in obj)        elif isinstance(obj, str): pass        elif isinstance(obj, (bytes, bytearray, memoryview)): if isinstance(obj, memoryview):     total += _sizeof(obj.obj, memo)        elif isinstance(obj, (set, frozenset)): total += sum(_sizeof(item, memo) for item in obj)        elif isinstance(obj, dict): total += sum(_sizeof(key, memo) + _sizeof(value, memo)   for key, value in obj.items())        elif hasattr(obj, '__slots__'): for name in obj.__slots__:     total += _sizeof(getattr(obj, name, obj), memo)        elif hasattr(obj, '__dict__'): total += _sizeof(obj.__dict__, memo)        else: raise TypeError('could not get size of {!r}'.format(obj))        return total    return _sizeof(variable, set())


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/668016.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号