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 · 2689 次点击
    这是一个创建于 1147 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大概是如下结构体
    [{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   ·   我们的愿景   ·   实用小工具   ·   3302 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 11:28 · PVG 19:28 · LAX 04:28 · JFK 07:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.