Python3 JSON库json、orjson、rapidjson loads和dumps速度对比

项目中需要将json字符串反序列化成Python字典对象,但发现原生的json库速度非常慢,就想使用第三方库取代它,发现了orjson和rapidjson两个比较流行的第三方库,测试一下

环境

  • Python 3.8.6
  • macOS 11.1
  • orjson 3.4.6
  • rapidjson 1.0

介绍

orjson

Fast, correct Python JSON library supporting dataclasses, datetimes, and numpy

号称Python下最快的json库,项目地址:https://github.com/ijl/orjson#install
源码显示底层使用了rust,难怪那么快
在这里插入图片描述

rapidjson

rapidjson是腾讯开源的json库,项目地址:https://github.com/Tencent/rapidjson
底层用的C++
在这里插入图片描述
Python中使用需要python封装库python-rapidjson,项目地址:https://github.com/python-rapidjson/python-rapidjson

安装

orjson

>>> pip3 install orjson
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting orjson
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/dc/be/96126a886573ebbb979d67f5bc4000acb7907042b8aa3494f70d118f9395/orjson-3.4.6-cp38-cp38-macosx_10_7_x86_64.whl (231 kB)
     |████████████████████████████████| 231 kB 1.5 MB/s
Installing collected packages: orjson
Successfully installed orjson-3.4.6

rapidjson

>>> pip3 install python-rapidjson
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting python-rapidjson
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fb/57/503810343cea48df3978ac4b34eb132c10204f0efa2a695d74c49b183004/python_rapidjson-1.0-cp38-cp38-macosx_10_9_x86_64.whl (202 kB)
     |████████████████████████████████| 202 kB 5.8 MB/s
Installing collected packages: python-rapidjson
Successfully installed python-rapidjson-1.0

对比

dumps:

import time
import json
import orjson
import rapidjson

m = {
    "timestamp": 1556283673.1523004,
    "task_uuid": "0ed1a1c3-050c-4fb9-9426-a7e72d0acfc7",
    "task_level": [1, 2, 1],
    "action_status": "started",
    "action_type": "main",
    "key": "value",
    "another_key": 123,
    "and_another": ["a", "b"],
}

def benchmark(name, dumps):
    start = time.time()
    for i in range(1000000):
        dumps(m)
    print(name, '{:.3f}'.format(time.time() - start))

benchmark("json:     ", json.dumps)
# orjson only outputs bytes, but often we need unicode:
benchmark("orjson:   ", lambda s: str(orjson.dumps(s), "utf-8"))
benchmark("rapidjson:", rapidjson.dumps)

loads:

import time
import json
import orjson
import rapidjson

m = '{"timestamp": 1556283673.1523004,"task_uuid": "0ed1a1c3-050c-4fb9-9426-a7e72d0acfc7","task_level": [1, 2, 1],"action_status": "started","action_type": "main","key": "value","another_key": 123,"and_another": [
"a", "b"]}'

def benchmark(name, loads):
    start = time.time()
    for i in range(1000000):
        loads(m)
    print(name, '{:.3f}'.format(time.time() - start))

benchmark("json:     ", json.loads)
# orjson only outputs bytes, but often we need unicode:
benchmark("orjson:   ", orjson.loads)
benchmark("rapidjson:", rapidjson.loads)

output:

jsonorjsonrapidjson
dumps5.3031.2092.751
loads4.0961.6002.075

参考:https://pythonspeed.com/articles/faster-json-library/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值