Python 数据结构内存占用是原始数据的 5 倍左右 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wohenfuyou
V2EX    Python

Python 数据结构内存占用是原始数据的 5 倍左右

  •  
  •   wohenfuyou 2021-02-05 14:38:08 +08:00 3414 次点击
    这是一个创建于 1710 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大概是如下结构体
    [{dic1....},{key1:[value1,value2...],key2:[value1,value2...]},{dic3},{dic4}]

    1.5g 数据要 7g 多内存,请问这是正常的内存占用么,要是优化的话在哪方面优化呢,请教各位~

    12 条回复    2021-02-07 20:18:06 +08:00
    linksNoFound
        1
    linksNoFound  
       2021-02-05 14:59:05 +08:00   1
    买内存
    lkytal
        2
    lkytal  
       2021-02-05 15:00:17 +08:00
    不用 Python (雾
    est
        3
    est  
       2021-02-05 15:18:13 +08:00
    一般是用 __slots__ 缓解。不过你遇到内存问题建议 numpy 。
    iqxd
        4
    iqxd  
       2021-02-05 15:29:43 +08:00   1
    大量小数据如果纠结内存就不要用通用数据结构。
    为了读取和插入效率,python 的 dict 的默认分配键值空间与实际键值数量大概是 3:1.
    对于 python 还算正常,即使是 C 的双向链表,用 64 位指针有时候占用内存都可能是实际数据的 3 倍
    ebingtel
        5
    ebingtel  
       2021-02-05 15:30:12 +08:00
    纯粹遍历的话 用生成器 别用 list
    gjquoiai
        6
    gjquoiai  
       2021-02-05 16:49:17 +08:00
    pandas 解君愁
    tmackan
        7
    tmackan  
       2021-02-05 18:00:47 +08:00
    py 也有基础类型的,除了 list,tuple,dict 以后,也支持特定类型的类型,可以看下,比如只支持 int 的 list
    tmackan
        8
    tmackan  
       2021-02-05 18:14:22 +08:00
    @tmackan 纠正下 from array import array 这个数组,只能指定特定类型的数组

    A Python array is as lean as a C array. When creating an array, you provide a typecode, a letter to determine the underlying C type used to store each item in the array. For example, b is the typecode for signed char. If you create an array('b'), then each item will be stored in a single byte and interpreted as an integer from 128 to 127. For large sequences of numbers, this saves a lot of memory. And Python will not let you put any number that does not match the type for the array.
    vance123
        9
    vance123  
       2021-02-05 18:18:27 +08:00
    虽然 Python 提供了比 C 更多的功能, 但这是以时间和空间为代价的. 以 str 为例, 单个对象起步就是几十 Byte 的 metadata, 字符串中只要有一个中文字符, 其内存占用都会比纯 ascii 多上 3 倍.
    lysS
        10
    lysS  
       2021-02-05 18:59:01 +08:00
    py 又不是 C,是这样的,我记得 PHP 的数组占用大小也是实际大小的 4 倍左右
    littleMaple
        11
    littleMaple  
       2021-02-06 02:17:12 +08:00   2
    感觉是个 XY 问题,题主如果可以给出更多上下文,可以更好地解决。

    如果是 homogeneous data structure,可以用楼上说的 numpy,pandas,array 等库。

    另外可以考虑转换成数据库数据,需要访问数据时再和数据库进行交互,就不必把所有数据都一下子挤进内存中。顺带一提 Python 有原生自带数据库交互库叫做 sqlite3.

    看你列出的数据的格式,似乎像是 JSON,可以用一些 lazy-parse json 的库,例如 ijson ( https://pypi.org/project/ijson/), json-streamer ( https://github.com/kashifrazzaqui/json-streamer), bigjson ( https://github.com/henu/bigjson), jsonslicer ( https://pypi.org/project/jsonslicer/).

    如果对精度要求不高,可以用一些 succinct data structure 来大大简化数据结构空间开销,例如布隆过滤器,HyperLogLog,Count-Min sketch 等。

    关于数据空间占用的优化,可以看经典的《 Python 高性能编程》,里面除了讲 Python 代码如何提升时间性能,还讲了 Python 代码如何提升空间性能,讲的非常全面细致,大部分可用的选项都覆盖了.
    hotea
        12
    hotea  
       2021-02-07 20:18:06 +08:00
    数据有重复值的话 categorical 可以省一点内存
    https://pandas.pydata.org/pandas-docs/stable/user_guide/categorical.html
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2716 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 12:03 PVG 20:03 LAX 05:03 JFK 08:03
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86