前面几篇文章总结了python中jsonschema与schema的用法,这里测试一下两者的效率:

上代码:

import time
from jsonschema import validate, draft7_format_checker
from jsonschema.exceptions import SchemaError, ValidationError
from schema import Schema, And, Optional, SchemaError, Regex def tags_check(tags_list):
if len(tags_list) < 1 or len(tags_list) > 5:
return False
for tag in tags_list:
if len(tag) < 2:
return False
return True def id_generator(start=1):
while 1:
yield start
start += 1 class DataFactory(object):
def __init__(self):
self.id_g = id_generator() def create_data(self):
idn = next(self.id_g)
price = 5.5 + idn
json_data = {
"id": idn,
"name": "jarvis手册%d" % idn,
"info": "贾维斯平台使用手册%d" % idn,
"price": price,
"tags": ["jar"],
"date": "2019-5-25",
"others": {
"info1": "1111",
"info2": "2222"
}
}
return json_data schema1 = {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "book info",
"description": "some information about book",
"type": "object",
"properties": {
"id": {
"description": "The unique identifier for a book",
"type": "integer",
"minimum": 1
},
"name": {
"description": "book name",
"type": "string",
"minLength": 3,
"maxLength": 30
},
"info": {
"description": "simple information about book",
"type": "string",
"minLength": 10,
"maxLength": 60
},
"price": {
"description": "book price",
"type": "number",
"multipleOf": 0.5,
"minimum": 5.0,
"maximum": 111111.0,
},
"tags": {
"type": "array",
"additonalItems": {
"type": "string",
"miniLength": 2
},
"miniItems": 1,
"maxItems": 5,
},
"date": {
"description": "书籍出版日期",
"type": "string",
"format": "date",
},
"bookcoding": {
"description": "书籍编码",
"type": "string",
"pattern": "^[A-Z]+[a-zA-Z0-9]{12}$"
},
"others": {
"description": "其他信息",
"type": "object",
"properties": {
"info1": {
"type": "string"
},
"info2": {
"type": "string"
}
}
}
},
"required": [
"id", "name", "info", "price", "tags"
]
} schema2 = {
"id": And(int, lambda x: 1 <= x, error="id必须是整数,大于等于100"),
"name": And(str, lambda s: 3 <= len(s) <= 30, error="name长度3-10"),
"info": And(str, lambda s: 10 <= len(s) <= 60, error="info信息出错"),
"price": And(float, lambda x: (5.0 < x < 111111.0) and (x % 0.5 == 0), error="price必须是大于5.0小于111.0的小数,且能被0.5整除"),
"tags": And(list, tags_check, error="tags出错"),
Optional("date"): And(str, error="日期格式出错"),
Optional("bookcoding"): And(str, Regex("^[A-Z]+[a-zA-Z0-9]{12}$"), error="书籍编码出错"),
Optional("others"): {
"info1": str,
"info2": str
},
} def time_jsonschema(data):
start_time = time.time()
for json_data in data:
try:
validate(instance=json_data, schema=schema1, format_checker=draft7_format_checker)
except SchemaError as e:
print("验证模式出错:{}\n提示信息:{}".format(" --> ".join([i for i in e.path]), e.message))
except ValidationError as e:
print("出错字段:{}\n提示信息:{}".format(" --> ".join([i for i in e.path]), e.message))
else:
continue
end_time = time.time()
return end_time - start_time def time_schema(data):
start_time = time.time()
for json_data in data:
try:
Schema(schema2).validate(json_data)
except SchemaError as e:
print(e)
else:
continue
end_time = time.time()
return end_time - start_time if __name__ == "__main__":
data = DataFactory()
data_list = [data.create_data() for i in range(10000)]
t1 = time_jsonschema(data_list)
t2 = time_schema(data_list)
print("jsonschema:schema = {}:{} = {}:1\n".format(t1, t2, t1/t2))

结果分析:

# 10条数据时:
jsonschema:schema = 0.012000083923339844:0.0019941329956054688 = 5.517694882831181:1
# 100条数据时:
jsonschema:schema = 0.10173273086547852:0.023936033248901367 = 4.180191742616664:1
# 1000条数据时:
jsonschema:schema = 0.9435069561004639:0.2263953685760498 = 4.127518805860752:1
# 10000条数据时:
jsonschema:schema = 9.319035053253174:2.2689626216888428 = 4.1371787451116295:1

数据在10条的时候,多次测验,最终结果不稳定,耗时比在6.0 ,5.5,3.6左右,波动较大。

数据在100条的时候,多次测验,最终结果比较稳定,耗时比在3.85—4.3之间

数据在1000条的时候,多次测验,最终结果的耗时比在4.0—4.2之间

数据在10000条的时候,由于每次测试时间都比较长,故测试数据相对比较少,但耗时比在4.1左右

试验次数不是很多,基于上面代码和测试数据,schema 效率比 jsonschema 大约高出 4倍

python中 jsonchema 与 shema 效率比较的更多相关文章

  1. Python 中,字符串"连接"效率最高的方式是?一定出乎你的意料

    网上很多文章人云亦云,字符串连接应该使用「join」方法而不要用「+」操作.说前者效率更高,它以更少的代价创建新字符串,如果用「+」连接多个字符串,每连接一次,就要为字符串分配一次内存,效率显得有点低 ...

  2. Python中的bool类型

    Python 布尔类型 bool python 中布尔值使用常量True 和 False来表示:注意大小写 比较运算符< > == 等返回的类型就是bool类型:布尔类型通常在 if 和 ...

  3. python中in在list和dict中查找效率比较

    转载自:http://blog.csdn.net/wzgbm/article/details/54691615 首先给一个简单的例子,测测list和dict查找的时间: ,-,-,-,-,,,,,,] ...

  4. python中列表的insert和append的效率对比

    python中insert和append方法都可以向列表中插入数据只不过append默认插入列表的末尾,insert可以指定位置插入元素. 我们来测试一下他俩插入数据的效率: 测试同时对一个列表进行插 ...

  5. 用 ElementTree 在 Python 中解析 XML

    用 ElementTree 在 Python 中解析 XML 原文: http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python- ...

  6. python中的反射

    在绝大多数语言中,都有反射机制的存在.从作用上来讲,反射是为了增加程序的动态描述能力.通俗一些,就是可以让用户参与代码执行的决定权.在程序编写的时候,我们会写很多类,类中又有自己的函数,对象等等.这些 ...

  7. python中协程

    在引出协成概念之前先说说python的进程和线程. 进程: 进程是正在执行程序实例.执行程序的过程中,内核会讲程序代码载入虚拟内存,为程序变量分配空间,建立 bookkeeping 数据结构,来记录与 ...

  8. 详解Python中的循环语句的用法

    一.简介 Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性.须重要理解,if.while.for以及与它们相搭配的 else. elif.break.continue和pass语句 ...

  9. Python 中的数据结构总结(一)

    Python 中的数据结构 “数据结构”这个词大家肯定都不陌生,高级程序语言有两个核心,一个是算法,另一个就是数据结构.不管是c语言系列中的数组.链表.树和图,还是java中的各种map,随便抽出一个 ...

随机推荐

  1. 剑指:链表中倒数第k个节点

    题目描述 输入一个链表,输出该链表中倒数第k个结点. 解法 pre 指针走 k-1 步.之后 cur 指针指向 phead,然后两个指针同时走,直至 pre 指针到达尾结点. 即cur与pre始终相距 ...

  2. .net web mvc 权限验证

    这里分享MVC的权限验证,内容中可能存在一些,莫名其妙的方法,那些是以前封装好的,大致可以根据方法名称知道他的意思. using Game.Entity; using Game.Entity.Plat ...

  3. Django框架(二)-- 基本配置:app注册、模板配置、静态文件配置、数据库连接配置post和get

    一.app 在Django中,APP可以用以下类比 大学 --------------------项目 计算机学院------------app01 土木学院 ------------ app02 1 ...

  4. Odoo search 搜索视图详解与搜索视图工作原理

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826430.html 搜索视图 搜索视图的search标签本身没什么属性可以使用,只要是<searc ...

  5. Websocket --socket.io的用法

    <!DOCTYPE html> <html> <head> <title>Hello WebSocket</title> <link ...

  6. Git常用基础命令

    要参与任何一个 Git 项目的协作,必须要了解该如何管理远程仓库.远程仓库是指托管在网络上的项目仓库,可能会有好多个,其中有些你只能读,另外有些可以写.同他人协作开发某 个项目时,需要管理这些远程仓库 ...

  7. NOIP 2003 栈

    洛谷 P1044 栈 洛谷传送门 JDOJ 1291: [NOIP2003]栈 T3 JDOJ传送门 题目描述 栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表. 栈有两 ...

  8. Gym - 102056C(2018EC final) -Heretical … Möbius ——CRT

    题意 给出一个长为200的01序列,判断是否在前1e9个莫比乌斯*值中.(这里的莫比乌斯值加了绝对值) 分析 意到因为4的倍数一定是0,9的倍数一定是0……169的倍数一定是0.那么我们可以对4,9, ...

  9. 【java异常】 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.emptech.db.demo.mapper.master.MOmQuotaTBMapper.findOmQuotaTB

    <mapper namespace="com.emptech.db.demo.mapper.master.MOmQuotaTBMapper"> public inter ...

  10. cocos2dx+KinectV2 体感游戏之微信打飞机

    https://download.csdn.net/download/qq_34609108/10038417 https://blog.csdn.net/qq_34609108/article/de ...