(免责声明:我读到的关于stackoverflow的每一个性能比较都会因为不全面/不正确/写得好/相关等等而受到抨击-我并不是假装这是一个真实的比较或完美的设置,我只是想知道我是否可以让cerberus更快地验证数据。)
我使用cerberus设置了以下模型:
v = Validator({
'id': {'type': 'integer', 'required': True},
'client_name': {'type': 'string', 'maxlength': 255, 'required': True},
'sort_index': {'type': 'float', 'required': True},
'client_phone': {'type': 'string', 'maxlength': 255, 'nullable': True},
'location': {
'type': 'dict',
'schema': {'latitude': {'type': 'float'}, 'longitude': {'type': 'float'}},
'nullable': True,
},
'contractor': {'type': 'integer', 'min': 0, 'nullable': True, 'coerce': int},
'upstream_http_referrer': {'type': 'string', 'maxlength': 1023, 'nullable': True},
'grecaptcha_response': {'type': 'string', 'minlength': 20, 'maxlength': 1000, 'required': True},
'last_updated': {'type': 'datetime', 'nullable': True, 'coerce': datetime_parse},
'skills': {
'type': 'list',
'default': [],
'schema': {
'type': 'dict',
'schema': {
'subject': {'type': 'string', 'required': True},
'subject_id': {'type': 'integer', 'required': True},
'category': {'type': 'string', 'required': True},
'qual_level': {'type': 'string', 'required': True},
'qual_level_id': {'type': 'integer', 'required': True},
'qual_level_ranking': {'type': 'float', 'default': 0, 'required': True},
},
},
},
})
...
def do_validation(data):
validated = v.validated(data)
if validated is None:
return False, v.errors
else:
return True, validated
这用于验证数据,大约50%的数据通过do_validation
。你知道吗
问题是cerberus在进行验证时非常慢,每次验证平均耗时超过一毫秒。相比之下,这比其他库慢10倍多,比我测试它的^{
如果cerberus的速度稍微慢一点,我也不会感到惊讶,但这种差异似乎太过极端,没有意义。你知道吗
我是不是做错了什么,严重影响了我的表现?你知道吗
上面的代码是为pydantic文档的benchmarks section编写的。你知道吗
PR adding cerberus(当前)是openhere,包含完整的代码和结果。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐