python中json的序列化与反序列化有很多库,具体选择使用哪一个,或者哪一个速度更快呢?

先上结果

json序列化与反序列化速度对比(按总时间排序:测试数据100 * 10000)

ujson           序列化: 2.084    反序列化: 1.157      总时间: 3.241
yajl 序列化: 1.910 反序列化: 1.970 总时间: 3.880
cjson 序列化: 3.305 反序列化: 1.328 总时间: 4.632
simplejson 序列化: 10.279 反序列化: 4.658 总时间: 14.937
stdlib json 序列化: 7.013 反序列化: 8.594 总时间: 15.607

其中,除了stdlib json也就是内置的json.dumps外,其他都是第三方包。数据量较少时,速度几乎没有区别,无所谓选择哪一个。数据量大的情况下,ujson的总体表现最好,但序列化不如yajl

而django中,如果只是response一个json对象,可以直接使用JsonResonse

用法为:

>>> from django.http import JsonResponse
>>> response = JsonResponse({'foo': 'bar'})
>>> response.content
'{"foo": "bar"}'

默认采用内置方式进json格式化后返回。如果数据不多,着实方便(django1.7引入)

测试代码

来自rtyler,在其基础上新增了ujson

import time

import pickle
import yajl try:
import cjson
except ImportError:
cjson = None
try:
import simplejson
except ImportError:
simplejson = None
try:
import ujson
except ImportError:
ujson = None try:
import json
except ImportError:
json = None default_data = {
"name": "Foo",
"type": "Bar",
"count": 1,
"info": {
"x": 203,
"y": 102, }, } def ttt(f, data=None, x=100 * 10000):
start = time.time()
while x:
x -= 1
foo = f(data)
return time.time() - start def profile(serial, deserial, data=None, x=100 * 10000):
if not data:
data = default_data
squashed = serial(data)
return (ttt(serial, data, x), ttt(deserial, squashed, x)) def test(serial, deserial, data=None):
if not data:
data = default_data
assert deserial(serial(data)) == data contenders = [
('yajl', (yajl.Encoder().encode, yajl.Decoder().decode)),
]
if cjson:
contenders.append(('cjson', (cjson.encode, cjson.decode)))
if simplejson:
contenders.append(('simplejson', (simplejson.dumps, simplejson.loads)))
if json:
contenders.append(('stdlib json', (json.dumps, json.loads)))
if ujson:
contenders.append(('ujson', (ujson.dumps, ujson.loads))) for name, args in contenders:
test(*args)
x, y = profile(*args)
print("%-11s serialize: %0.3f deserialize: %0.3f total: %0.3f" % (
name, x, y, x + y))

作者:二二向箔
链接:https://www.jianshu.com/p/c90f5b471e99
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

python中5个json库的速度对比的更多相关文章

  1. 【转】python 历险记(四)— python 中常用的 json 操作

    [转]python 历险记(四)— python 中常用的 json 操作 目录 引言 基础知识 什么是 JSON? JSON 的语法 JSON 对象有哪些特点? JSON 数组有哪些特点? 什么是编 ...

  2. Python中导入第三方声源库Acoular的逻辑解释以及Acoular的下载

    [声明]欢迎转载,但请保留文章原始出处→_→ 秦学苦练:http://www.cnblogs.com/Qinstudy/ 文章来源:http://www.cnblogs.com/Qinstudy/p/ ...

  3. 使用ctypes在Python中调用C++动态库

    使用ctypes在Python中调用C++动态库 入门操作 使用ctypes库可以直接调用C语言编写的动态库,而如果是调用C++编写的动态库,需要使用extern关键字对动态库的函数进行声明: #in ...

  4. Python中使用模块和库编程

    """ python中使用模块和库编程 导入模块 import modulename [as alias] from modulename import fun1,fun ...

  5. python 历险记(四)— python 中常用的 json 操作

    目录 引言 基础知识 什么是 JSON? JSON 的语法 JSON 对象有哪些特点? JSON 数组有哪些特点? 什么是编码和解码? 常用的 json 操作有哪些? json 操作需要什么库? 如何 ...

  6. python中精确输出JSON浮点数的方法

    有时需要在JSON中使用浮点数,比如价格.坐标等信息.但python中的浮点数相当不准确, 例如下面的代码: 复制代码代码如下: #!/usr/bin/env python import json a ...

  7. python接口自动化(九)--python中字典和json的区别(详解)

    简介 这篇文章的由来是由于上一篇发送post请求的接口时候,参数传字典(dict)和json的缘故,因为python中,json和dict非常类似,都是key-value的形式,为啥还要这么传参,在群 ...

  8. python中字典和json的区别

    python中,json和dict非常类似,都是key-value的形式,而且json.dict也可以非常方便的通过dumps.loads互转 定义 python中,json和dict非常类似,都是k ...

  9. python中eval()和json.dumps的使用

    在python中通过requests.get(url)获取json数据,此时可能需要eval进行解析. # -*- coding: utf-8 -*- import requests r = requ ...

随机推荐

  1. [七月挑选]使用hexo建立主题,并发布到github

    title: 使用hexo建立主题,并发布到github 根据hexo官网的概述和hexo官网的建站,搭建最开始的hexo博客. 1.环境预先安装好node.js和git 2.npm安装hexo: $ ...

  2. MySQL索引的分类、结构、使用场景

    MySQL索引分类 1.主键索引:设定为主键后数据库会自动建立索引,innodb为聚簇索引 语法: 随表一起建索引: CREATE TABLE customer (id INT(10) UNSIGNE ...

  3. TreeView详细用法

    Treeview用于显示按照树形结构进行组织的数据.          Treeview控件中一个树形图由节点(TreeNode)和连接线组成.TtreeNode是TTreeview的基本组成单元. ...

  4. 创建kudu数据集测试总结

    参考文档: https://cloud.tencent.com/developer/article/1474797 https://www.tgshenghe.com/a77nr1/nzt9t1.ht ...

  5. CPU指令重排序与MESI缓存一致性

    一.重排序场景 class ResortDemo { int a = 0; boolean flag = false; public void writer() { a = 1; //1 flag = ...

  6. Vue this.$nextTick原理

    虽然 Vue.js 通常鼓励开发人员沿着“数据驱动”的方式思考,避免直接接触 DOM,但是有时我们确实要这么做.比如一个新闻滚动的列表项.如果在这里需要操作dom, 应该是等待 Vue 完成更新 DO ...

  7. <转> thinkPHP的常用配置项2

    'URL_PATHINFO_DEPR'=>'-',//修改URL的分隔符'TMPL_L_DELIM'=>'<{', //修改左定界符'TMPL_R_DELIM'=>'}> ...

  8. bzoj3754 Tree之最小方差树 最小生成树+推性质

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3754 题解 感觉这个思路挺神仙的. 后悔没有好好观察题目的数据范围,一直把 \(n\) 和 \ ...

  9. mac下安装 rabbitMq

    1.安装HomeBrew,如果已经安装这一步跳过. 2.用brew install rabbitmq指令即可进行rabbitmq服务的自动安装. 3.安装完成之后会出现一下提示:   rabbit安装 ...

  10. Spring Boot 2.x整合mybatis及druid数据源及逆向工程

    1逆向工程 1)db.properties #============================# #===== Database sttings =====# #=============== ...