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
DongDongXie
V2EX  ›  Python

Python 里面 np.savetxt()和 pickle 本地化时,两个性能对比怎么样

  •  
  •   DongDongXie · 2018-11-26 17:08:57 +08:00 · 2453 次点击
    这是一个创建于 1949 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题,数据量比较大,想把一个 ndarray 保存到本地,这两种方法和直接 write 到本地性能相比怎么样,我试了下 savetxt()有点慢,请问大家有什么比较好的方法吗

    6 条回复    2018-11-27 15:48:44 +08:00
    glasslion
        1
    glasslion  
       2018-11-26 17:20:47 +08:00
    试试 Apache Arrow(Apache Parquet Format)
    xpresslink
        2
    xpresslink  
       2018-11-26 18:30:06 +08:00
    cPickle 了解一下
    DongDongXie
        3
    DongDongXie  
    OP
       2018-11-26 19:54:04 +08:00
    @xpresslink 以前用过这个,据说比 pickle 快
    deepreader
        4
    deepreader  
       2018-11-27 11:47:00 +08:00
    massage pack 了解一下
    wwqgtxx
        5
    wwqgtxx  
       2018-11-27 15:22:36 +08:00
    明明就有正宗的 np.save
    https://docs.scipy.org/doc/numpy/reference/generated/numpy.save.html
    非要用其他的序列化方法干嘛
    wwqgtxx
        6
    wwqgtxx  
       2018-11-27 15:48:44 +08:00
    其实还有一些蹊跷意淫的办法,比如你先 pickle 保存 ndarray 的 dtype size shape 三个属性,然后用 memcpy 直接保存 ndarray 的 data,那是一个 memoryview 对象,你可以用 mmap 或者直接 file.write 保存进文件,读的时候用 np.frombuffer 或者 np.fromstring 读进来后 reshape 一下就好了( frombuffer 的话最后需要 copy 一下,否则当原 buffer 对象失效之后会出问题,当然如果你原 buffer 对象是从 mmap 获得的话那也没事,而且后续对该 ndarray 的修改还能用过 mmap.flush 写回硬盘)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2813 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 15:26 · PVG 23:26 · LAX 08:26 · JFK 11:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.