Json是开发中经常用到的一种轻量级数据交换格式。我们经常用到的Json模块有json simplejson flask.json ujson。他们之间性能有多大差距呢?让我们来做个实验对比。
测试项目总共包含四项:
- 简单Json字符解码
- 复杂Json字符解码
- 简单Json对象编码
- 复杂Json对象编码
测试环境:Python 3.5.2 macOS 10.12.1 RMBP 13 Early 2015
测试代码开源于Github
通过运行测试,我们得到了以下数据(各执行10万次):
| simple_load | complex_load | simple_dump | complex_dump | |
|---|---|---|---|---|
| json | 0.36s | 3.50s | 0.53s | 5.72s |
| simplejson | 0.44s | 3.37s | 0.71s | 8.41s |
| flask.json | 5.80s | 4.37s | 1.83s | 13.55s |
| ujson | 0.08s | 2.60s | 0.08s | 2.66s |
可以看出,ujson的性能最佳,json和simplejson的性能较为接近,flask.json的性能则比较落后了
结论:推荐使用ujson模块。