Python更快的解析JSON大文件
2017-10-27 01:03
579 查看
提出问题
今天用python的simplejson库解析一个 >200MB 的JSON文件,发现一次decode/encode都得要 >10s,这个在我开来,实在太慢了,有没有更快的库了?
先给出我的简单测试结果
json大小:245MB
测试方法:read文件内容,然后一次decode, 一次encode
不成熟的结论: pypy+json最快
方法一:pypy+json
python自带的JSON库是用纯python代码实现的,而pypy对纯python代码的加速效果比较好。至于为什么,大家可以去google吧,很多文章解释的很好。
方法二:UltraJson
我首先想到的用C库来做JSON的解析,原因你懂的,而C语言有个JSON库叫CJSON,于是用python+cjson在google里找到了UltraJson
UltraJson是作者用C语言实现的JSON库,实际测试的效果是,整个encode的效率提升了2倍多。
使用方法:
安装:pip instal ujson
一些测试
Test machine:
Linux 3.13.0-66-generic x86_64 #108-Ubuntu SMP Wed Oct 7 15:20:27 UTC 2015
Versions:
CPython 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2]
blist : 1.3.6
simplejson: 3.8.1
ujson : 1.34 (0c52200eb4e2d97e548a765d5f089858c41967b0)
yajl : 0.3.5
Array with 256 UTF-8 strings
Array with 256 UTF-8 strings
Array with 256 strings
Medium complex object
Array with 256 True values
Array with 256 dict{string, int} pairs
Dict with 256 arrays with 256 dict{string, int} pairs
Dict with 256 arrays with 256 dict{string, int} pairs, outputting sorted keys
Complex object
今天用python的simplejson库解析一个 >200MB 的JSON文件,发现一次decode/encode都得要 >10s,这个在我开来,实在太慢了,有没有更快的库了?
先给出我的简单测试结果
json大小:245MB
测试方法:read文件内容,然后一次decode, 一次encode
解释器 | simplejson | json | ujson |
---|---|---|---|
pypy | 40s多 | 10s | 无 |
cpython | 12s多 | 17s多 | 10s多 |
方法一:pypy+json
python自带的JSON库是用纯python代码实现的,而pypy对纯python代码的加速效果比较好。至于为什么,大家可以去google吧,很多文章解释的很好。
方法二:UltraJson
我首先想到的用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]
一些测试
Test machine:
Linux 3.13.0-66-generic x86_64 #108-Ubuntu SMP Wed Oct 7 15:20:27 UTC 2015
Versions:
CPython 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2]
blist : 1.3.6
simplejson: 3.8.1
ujson : 1.34 (0c52200eb4e2d97e548a765d5f089858c41967b0)
yajl : 0.3.5
Array with 256 UTF-8 strings
Object | ujson | yajl | simplejson | json |
---|---|---|---|---|
encode | 3508.19 | 5742.00 | 3232.38 | 3309.09 |
decode | 25103.37 | 11257.83 | 11696.26 | 11871.04 |
Object | ujson | yajl | simplejson | json |
---|---|---|---|---|
encode | 3189.71 | 2717.14 | 2006.38 | 2961.72 |
decode | 1354.94 | 630.54 | 356.35 | 344.05 |
Object | ujson | yajl | simplejson | json |
---|---|---|---|---|
encode | 18127.47 | 12537.39 | 12541.23 | 20001.00 |
decode | 23264.70 | 12788.85 | 25427.88 | 9352.36 |
Object | ujson | yajl | simplejson | json |
---|---|---|---|---|
encode | 10519.38 | 5021.29 | 3686.86 | 4643.47 |
decode | 9676.53 | 5326.79 | 8515.77 | 3017.30 |
Object | ujson | yajl | simplejson | json |
---|---|---|---|---|
encode | 105998.03 | 102067.28 | 44758.51 | 60424.80 |
decode | 163869.96 | 78341.57 | 110859.36 | 115013.90 |
Object | ujson | yajl | simplejson | json |
---|---|---|---|---|
encode | 13471.32 | 12109.09 | 3876.40 | 8833.92 |
decode | 16890.63 | 8946.07 | 12218.55 | 3350.72 |
Object | ujson | yajl | simplejson | json |
---|---|---|---|---|
encode | 50.25 | 46.45 | 13.82 | 29.28 |
decode | 33.27 | 22.10 | 27.91 | 10.43 |
Object | ujson | yajl | simplejson | json |
---|---|---|---|---|
encode | 27.19 | 7.75 | 2.39 |
Object | ujson | yajl | simplejson | json |
---|---|---|---|---|
encode | 577.98 | 387.81 | 470.02 | |
decode | 496.73 | 234.44 | 151.00 | 145.16 |
相关文章推荐
- python解析json文件读取Android permission说明
- python解析json文件回顾(load/loads)
- python解析json文件
- python中解析json格式文件的方法示例
- 使用python解析json文件 分类: python 小练习 2014-03-21 18:17 354人阅读 评论(0) 收藏
- Python解析json文件相关知识学习
- 使用python解析json文件
- Python操作小结(连接mysql、解析txt\json文件、crontab任务、http请求, 等等)
- 利用python解析json文件
- python解析json字符串和json文件的区别
- Python 获取接口数据,解析JSON,写入文件
- Python解析json文件报错:'utf8' codec can't decode byte 0xbb in position 0: invalid start byte
- Python解析json文件报错:'utf8' codec can't decode byte 0xbb in position 0: invalid start byte
- python解析json文件
- Python 获取接口数据,解析JSON,写入文件
- 使用python解析json文件
- Python-读入json文件并进行解析及json基本操作
- python解析jSON文件
- 使用python解析json文件(转)
- 使用Python解析JSON文件