提出問題
今天用python的******json庫解析乙個 >200mb 的json檔案,發現一次decode/encode都得要 >10s,這個在我開來,實在太慢了,有沒有更快的庫了?
先給出我的簡單測試結果
json大小:245mb
測試方法:read檔案內容,然後一次decode, 一次encode
直譯器******json
json
ujson
pypy
40s多
10s無
cpython
12s多
17s多
10s多
不成熟的結論: 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
>>> import ujson
>>> ujson.dumps([, 81, true])
'[,81,true]'
>>> ujson.loads("""[, 81, true]""")
[, 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
******json: 3.8.1
ujson : 1.34 (0c52200eb4e2d97e548a765d5f089858c41967b0)
yajl : 0.3.5
array with 256 utf-8 strings
object
ujson
yajl
******json
json
encode
3508.19
5742.00
3232.38
3309.09
decode
25103.37
11257.83
11696.26
11871.04
array with 256 utf-8 strings
object
ujson
yajl
******json
json
encode
3189.71
2717.14
2006.38
2961.72
decode
1354.94
630.54
356.35
344.05
array with 256 strings
object
ujson
yajl
******json
json
encode
18127.47
12537.39
12541.23
20001.00
decode
23264.70
12788.85
25427.88
9352.36
medium complex object
object
ujson
yajl
******json
json
encode
10519.38
5021.29
3686.86
4643.47
decode
9676.53
5326.79
8515.77
3017.30
array with 256 true values
object
ujson
yajl
******json
json
encode
105998.03
102067.28
44758.51
60424.80
decode
163869.96
78341.57
110859.36
115013.90
array with 256 dict pairs
object
ujson
yajl
******json
json
encode
13471.32
12109.09
3876.40
8833.92
decode
16890.63
8946.07
12218.55
3350.72
dict with 256 arrays with 256 dict pairs
object
ujson
yajl
******json
json
encode
50.25
46.45
13.82
29.28
decode
33.27
22.10
27.91
10.43
dict with 256 arrays with 256 dict pairs, outputting sorted keys
object
ujson
yajl
******json
json
encode
27.19
7.75
2.39
complex object
object
ujson
yajl
******json
json
encode
577.98
387.81
470.02
decode
496.73
234.44
151.00
145.16
Python更快的解析JSON大檔案
今天用python的 json庫解析乙個 200mb 的json檔案,發現一次decode encode都得要 10s,這個在我開來,實在太慢了,有沒有更快的庫了?直譯器 json json ujson pypy 40s多 10s無 cpython 12s多 17s多 10s多 根據下面的bench...
python中的json解析
主要實現以下功能 解析 與構造json,即encoder and decoder 官方指導 中文教程 前者將obj轉化為json str,後者將str轉化為python物件,如果json字串是個object,轉化為dict,若是array則轉化為list json寫法 表示array的json字串 ...
python 解析多層json
原始檔案內容 channels 3 height 1080 width 1920 修改並儲存 coding utf 8 import os import json 獲取目標資料夾的路徑 filedir r j numberdata mrcnnhik test 獲取資料夾中的檔名稱列表 filenam...