sol229 发表于 2018-8-9 06:56:08

Python更快的解析JSON大文件

  今天用python的simplejson库解析一个 >200MB 的JSON文件,发现一次decode/encode都得要 >10s,这个实在太慢了,有没有更快的库了?
  先给出我的简单测试结果
  

[*]  json大小:245MB
[*]  测试方法:read文件内容,然后一次decode, 一次encode
解释器simplejsonjsonujsonpypy40s多10s无cpython12s多17s多10s多  不成熟的结论: pypy+json最快
  方法一:pypy+json
  
  python自带的JSON库是用纯python代码实现的,而pypy对纯python代码的加速效果比较好。至于为什么,大家可以去google吧,很多文章解释的很好。
  
  方法二:UItraJson
  
  我首先想到的用C库来做JSON的解析,原因你懂的,而C语言有个JSON库叫CJSON,于是用python+cjson在google里找到了UltraJson
  UltraJson是作者用C语言实现的JSON库,实际测试的效果是,整个encode的效率提升了2倍多。
  
  使用方法
  安装:pip instal ujson
    >>> import ujson  >>> ujson.dumps([{"key": "value"}, 81, True])
  '[{"key":"value"},81,true]'
  >>> ujson.loads("""[{"key": "value"}, 81, true]""")
  [{u'key': u'value'}, 81, True]
  并不是所欲情况下都适合
  根据下面的BenchMark,在double数组的情况下,yajl的encode速度是比UltraJson的,所以,如果你的JSON文件较小的话,其实无所谓哪个库,如果是像我这样的大JSON文件,可以根据下面的表选择合适的JSON库。
  BenchMark
  下面是作者给出的benchmark:表格中的数字是每秒的调用次数,也就是说,数字越大,表示效率越高。
Versions:

[*]  CPython 2.7.6 (default, Jun 22 2015, 17:58:13)
[*]  blist : 1.3.6
[*]  simplejson: 3.8.1
[*]  ujson : 1.34 (0c52200eb4e2d97e548a765d5f089858c41967b0)
[*]  yajl : 0.3.5
ujsonyajlsimplejsonjsonArray with 256 doublesencode3508.1957423232.383309.09decode25103.3711257.8311696.2611871.04Array with 256 UTF-8 stringsencode3189.712717.142006.382961.72decode1354.94630.54356.35344.05Array with 256 stringsencode18127.4712537.3912541.2320001decode23264.712788.8525427.889352.36Medium complex objectencode10519.385021.293686.864643.47decode9676.535326.798515.773017.3Array with 256 True valuesencode105998.03102067.2844758.5160424.8decode163869.9678341.57110859.36115013.9Array with 256 dict{string, int} pairsencode13471.3212109.093876.48833.92decode16890.638946.0712218.553350.72Dict with 256 arrays with 256 dict{string,int} pairsencode50.2546.4513.8229.28decode33.2722.127.9110.43Dict with 256 arrays with 256 dict{string,int} pairs, outputting sorted keysencode27.197.752.39Complex objectencode577.98387.81470.02decode496.73234.44151145.16Versions:

[*]  CPython 3.4.3 (default, Oct 14 2015, 20:28:29)
[*]  blist : 1.3.6
[*]  simplejson: 3.8.1
[*]  ujson : 1.34 (0c52200eb4e2d97e548a765d5f089858c41967b0)
[*]  yajl : 0.3.5
ujsonyajlsimplejsonjsonArray with 256 doublesencode3477.155732.243016.763071.99decode23625.29731.459501.579901.92Array with 256 UTF-8 stringsencode1995.892151.611771.981817.2decode1425.04625.38327.14305.95Array with 256 stringsencode25461.7512188.613054.7614429.81decode21981.3117014.223869.4822483.58Medium complex objectencode10821.464837.043114.044254.46decode7887.775126.674934.66204.97Array with 256 True valuesencode100452.994639.446657.6360358.63decode148312.775485.988434.91116395.5Array with 256 dict{string, int} pairsencode11698.138886.963043.696302.35decode10686.47061.775646.87702.29Dict with 256 arrays with 256 dict{string,int} pairsencode44.2634.4310.421.97decode28.4623.9518.722.83Dict with 256 arrays with 256 dict{string,int} pairs, outputting sorted keysencode33.66.9422.34Complex objectencode432.3351.47379.34decode434.4221.97149.57147.79  结尾
  通过上面的这些步骤其实就可以解析JSON大文件了,是不是很简单呢?
  原文链接:https://segmentfault.com/a/1190000010138782
课程推荐
  
  Golang 实战班第2期火热报名进行中
  
  招生要求:
  
  有 Linux 基础,有志于使用 Go 语言做分布式系统编程的人员,想往系统架构师方向发展的同学。BAT 架构师带你一起飞。
  课程内容:
  

[*]  Golang入门
[*]  Golang程序结构
[*]  Golang的基础数据类型
[*]  Golang复合数据类型
[*]  Golang的函数
[*]  Golang的方法
[*]  Golang的接口
[*]  Golang的协程和Channel
[*]  Golang基于共享变量的并发
[*]  Golang包和工具
  上课模式:网络直播班    线下面授班
  咨询报名联系:
  QQ(1):979950755    小月
  QQ(2):279312229    ada
  WeChat : 1902433859   小月
  WeChat : 1251743084   小单
  开课时间:10月14日(周六)
  课程大纲:http://51reboot.com/course/go
页: [1]
查看完整版本: Python更快的解析JSON大文件