Python 處理 JSON 我選擇 ujson 和 orjson
在 Python 使用用 json.dumps(data) 時(shí)突然發(fā)現(xiàn)特別慢,data 本身不大,但是一個(gè)包含很多元素的列表,所以促使本人尋找一個(gè)替代的 JSON 處理庫(kù)。大概對(duì)比了一個(gè) ujson(UtltraJSON), python-rapidjson(RapidJSON) 和 Python 自帶的 json 庫(kù)。還有一個(gè) simplejson 是為兼容 Python 2.6 以前用的(json 是 Python 2.6 新加入的 API),性能有些差。
基本上姜還是老的辣,想要收獲更好的性能,還得仰賴傳統(tǒng)的 C/C++ 語言,ujson 是用純 C 寫的,RapidJSON 是 C++ 寫的,后者還是十美分的開源產(chǎn)品。json, ujson, rapidjson 三者 loads() 方法的性能差別不太明顯,但 dumps() 大對(duì)象時(shí) Python 自帶的 json 庫(kù)就要考驗(yàn)用戶的耐心了。
注:最開始本來認(rèn)定了 ujson 為最佳選擇, 所以先從 usjon 和 rapidjson 切入的,后來寫作本文的過程中,從 usjon 的自我介紹中發(fā)現(xiàn)了 Rust 寫的 orjson 很顯眼,才加入了 orjson 的測(cè)試,看來 orjson 更值得擁有。
以上三個(gè) json 組件的安裝方式分別為
pip install python-rapidjson
$ pip install simplejson
自己測(cè)試了一個(gè) ujson 和 rapidjson 與 Python json 庫(kù)的 dumps() 的性能,simpejson 不太考慮了。測(cè)試代碼如下
- # test.py
- from time import time
- import sys
- import string
- num = int(sys.argv[1])
- lib = sys.argv[2]
- items = []
- for i in range(num):
- items.append({c:c for c in string.ascii_letters})
- start = time()
- if lib == 'ujson':
- import ujson
- ujson.dumps(items)
- elif lib == 'rapidjson':
- import rapidjson
- rapidjson.dumps(items)
- else:
- import json
- json.dumps(items)
- print(time() - start)
執(zhí)行 python 1000|10000|100000|1000000 json|ujson|rapidjson, 試結(jié)果統(tǒng)計(jì)如下(數(shù)字為不同情況下的耗時(shí)):
基本上測(cè)試的性能和 Benchmark of Python JSON libraries 中的是一致的。從原文中截取了兩張圖如下:
在 UltraJSON 的 Github 項(xiàng)目頁(yè)面中也有對(duì)比 ujson, nujson, orjson, simplejson, json 的 Benchmarks。其中列出的 orjson(pip install orjson) 和 nujson(pip install nujson, Fork 了 UltraJSON 來支持 Numpy 序列化的) 性能表現(xiàn)上不錯(cuò),orjson 表現(xiàn)上比 ujson 還更為卓越。
看到了 orjson 后,趕緊做個(gè)對(duì)比測(cè)試,在上面的 test.py 代碼中再加上
- elif lib == 'orjson':
- import orjson
- orjson.dumps(items)
再列出完整的對(duì)比數(shù)據(jù)
繼續(xù)翻看 orjson 的 Github 主頁(yè)面 ijl/orjson, 它既非用 C 也不是用 C++ 寫的,而是 Rust 語言,真是讓我眼前一亮,Rust 程序運(yùn)行速度真的能與 C/C++ 相媲美的。寫到這里我要開始改變當(dāng)初只認(rèn) ujson 的主意了,orjson 或許是更佳的選擇, 本文的標(biāo)題也由最初擬定的 “Python 處理 JSON 必要時(shí)我選擇 ujson(UltraJSON)” 變成了 “Python 處理 JSON 必要時(shí)我選擇 ujson 和 orjson”。這也是寫博客時(shí),盡可能收集更多的素材多的魅力。
補(bǔ)充一下,orjson 的 dumps() 函數(shù)使用略有不同,不再用 indent 參數(shù),并且返回值是 bytes,所以格式化成字符串的寫法如下
- import orjson
- json_str = orjson.dumps(record, option=orjson.OPT_INDENT_2).decode()
另外,在使用 ujson 時(shí)碰到的一個(gè) bug 也順便記錄在此,就不立新篇了,反正現(xiàn)在找東西都不太看標(biāo)題,而是 Google 到其中的內(nèi)容。ujson 3.0.0 和 3.1.0 版本的 dumps() 的 indent 參數(shù)工作不正常,有個(gè)未關(guān)閉的 ticket 'indent' parameter for dumps doesn't indent properly in 3.0.0 #415。比如使用 ujson 3.1.0 時(shí)的現(xiàn)像是
- >>> import ujson
- >>> ujson.dumps({'a': 1, 'b': 2})
- '{"a":1,"b":2}'
- >>> ujson.dumps({'a': 1, 'b': 2}, indent=0)
- '{"a":1,"b":2}'
- >>> ujson.dumps({'a': 1, 'b': 2}, indent=1)
- '{\n "a": 1,\n "b": 2\n}'
- >>> ujson.dumps({'a': 1, 'b': 2}, indent=2)
- '{\n "a": 1,\n "b": 2\n}'
- >>> ujson.dumps({'a': 1, 'b': 2}, indent=8)
- '{\n "a": 1,\n "b": 2\n}'
indent 大于 1 時(shí)都當(dāng)作 1。
換回到 ujson 2.0.3 版本時(shí)沒問題
- >>> import ujson
- >>> ujson.dumps({'a': 1, 'b': 2}, indent=2)
- '{\n "a": 1,\n "b": 2\n}'
- >>> ujson.dumps({'a': 1, 'b': 2}, indent=8)
- '{\n "a": 1,\n "b": 2\n}'
在這個(gè)問題未解決之前就暫時(shí)用 pip install ujson==2.0.3 安裝 ujson 2.0.3 吧,但是這個(gè)版本無法序列化 datetime 類型。