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

先上结果

  1. json序列化与反序列化速度对比(按总时间排序:测试数据100 * 10000
  2. ujson 序列化: 2.084 反序列化: 1.157 总时间: 3.241
  3. yajl 序列化: 1.910 反序列化: 1.970 总时间: 3.880
  4. cjson 序列化: 3.305 反序列化: 1.328 总时间: 4.632
  5. simplejson 序列化: 10.279 反序列化: 4.658 总时间: 14.937
  6. stdlib json 序列化: 7.013 反序列化: 8.594 总时间: 15.607

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

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

用法为:

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

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

测试代码

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

  1. import time
  2. import pickle
  3. import yajl
  4. try:
  5. import cjson
  6. except ImportError:
  7. cjson = None
  8. try:
  9. import simplejson
  10. except ImportError:
  11. simplejson = None
  12. try:
  13. import ujson
  14. except ImportError:
  15. ujson = None
  16. try:
  17. import json
  18. except ImportError:
  19. json = None
  20. default_data = {
  21. "name": "Foo",
  22. "type": "Bar",
  23. "count": 1,
  24. "info": {
  25. "x": 203,
  26. "y": 102, }, }
  27. def ttt(f, data=None, x=100 * 10000):
  28. start = time.time()
  29. while x:
  30. x -= 1
  31. foo = f(data)
  32. return time.time() - start
  33. def profile(serial, deserial, data=None, x=100 * 10000):
  34. if not data:
  35. data = default_data
  36. squashed = serial(data)
  37. return (ttt(serial, data, x), ttt(deserial, squashed, x))
  38. def test(serial, deserial, data=None):
  39. if not data:
  40. data = default_data
  41. assert deserial(serial(data)) == data
  42. contenders = [
  43. ('yajl', (yajl.Encoder().encode, yajl.Decoder().decode)),
  44. ]
  45. if cjson:
  46. contenders.append(('cjson', (cjson.encode, cjson.decode)))
  47. if simplejson:
  48. contenders.append(('simplejson', (simplejson.dumps, simplejson.loads)))
  49. if json:
  50. contenders.append(('stdlib json', (json.dumps, json.loads)))
  51. if ujson:
  52. contenders.append(('ujson', (ujson.dumps, ujson.loads)))
  53. for name, args in contenders:
  54. test(*args)
  55. x, y = profile(*args)
  56. print("%-11s serialize: %0.3f deserialize: %0.3f total: %0.3f" % (
  57. 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. Axiso解决跨域访问

    问题: 在项目中需要需要讲本地项目去请求一个URL接口获取数据 例如: 本地请求地址:http://127.0.0.1:19323/site/info.json 请求Url地址:http://www. ...

  2. 使用Cookie记住登录用户

    在登录表单中,写入: 记住我: <select name="cookie">       <option value="0" selected ...

  3. Linux下载:wget、yum与apt-get用法及区别

    一般来说著名的linux系统基本上分两大类: RedHat系列:Redhat.Centos.Fedora等 Debian系列:Debian.Ubuntu等 RedHat 系列 常见的安装包格式 rpm ...

  4. Keycode含义

    keycode 是键盘上每一个按键对应的码keycode如下 :keycode 0 = keycode 1 = keycode 2 = keycode 3 = keycode 4 = keycode ...

  5. 1118. Birds in Forest (25)

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  6. nginx的配置和基本参数说明

    16 117 118 119 120 121 122 123 #运行用户 user nobody; #启动进程,通常设置成和cpu的数量相等 worker_processes  1;   #全局错误日 ...

  7. easyui自学模板代码

    index.jsp源码 <%@ page language="java" contentType="text/html; charset=UTF-8" p ...

  8. react native 实现TODO APP

    前端有一个todo app非常适合入门练手 react-native 实现todo app:https://github.com/nwgdegitHub/TODO_RN.git

  9. computed属性和watcher

    computed属性 在模板中使用表达式是非常方便直接的,然而这只适用于简单的操作.在模板中放入太多的逻辑,会使模板过度膨胀和难以维护.例如: <div id="example&quo ...

  10. Bugku 杂项 宽带信息泄露

    宽带信息泄露 flag是宽带用户名 下载文件后用RouterPassView打开,搜索username即可