Skip to content

Instantly share code, notes, and snippets.

@queertypes
Created October 1, 2013 16:39
Show Gist options
  • Save queertypes/6781406 to your computer and use it in GitHub Desktop.
Save queertypes/6781406 to your computer and use it in GitHub Desktop.
A series of microbenchmarks for jsonschema in Pythons (2.7.5, 3.3.2, pypy-2.1).
# Microbenchmarks using jsonschema in Python (2.7.5, 3.3.2) and pypy 2.1
## Common code
import jsonschema
schema = {
'type': 'object',
'properties': {
'weight': {'type': 'integer', 'minimum': 1},
'hosts': {'type': 'array', 'minItems': 1, 'items':
{'type': 'string'}
}
}
## pypy 2.1
### JIT warm up run
In [6]: %timeit jsonschema.validate({'weight': 1}, schema)
100 loops, best of 3: 1.63 ms per loop
In [7]: %timeit jsonschema.validate({'weight': 1}, schema)
1000 loops, best of 3: 259 µs per loop
In [10]: validator = jsonschema.Draft4Validator(schema)
In [11]: %timeit validator.validate({'weight': 1})
10000 loops, best of 3: 7.88 µs per loop
In [12]: %timeit validator.validate({'weight': 1})
10000 loops, best of 3: 7.93 µs per loop
## Python 3.3.2
In [3]: %timeit jsonschema.validate({'weight': 1}, schema)
1000 loops, best of 3: 1.44 ms per loop
In [4]: %timeit jsonschema.validate({'weight': 1}, schema)
1000 loops, best of 3: 1.44 ms per loop
In [5]: validator = jsonschema.Draft4Validator(schema)
In [6]: %timeit validator.validate({'weight': 1})
10000 loops, best of 3: 32.9 us per loop
In [7]: %timeit validator.validate({'weight': 1})
10000 loops, best of 3: 33.1 us per loop
## Python 2.7.5
In [3]: %timeit jsonschema.validate({'weight': 1}, schema)
1000 loops, best of 3: 1.14 ms per loop
In [4]: validator = jsonschema.Draft4Validator(schema)
In [5]: %timeit validator.validate({'weight': 1})
10000 loops, best of 3: 35.3 µs per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment