Skip to content

Instantly share code, notes, and snippets.

@AtnNn
Last active December 27, 2015 15:18
Show Gist options
  • Save AtnNn/7346211 to your computer and use it in GitHub Desktop.
Save AtnNn/7346211 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import rethinkdb as r
import rethinkdb.ast
import rethinkdb.net
import sys
import types
import json
import ujson
from timeit import timeit
json_str = sys.argv[1]
c = r.connect()
q1 = (r.json(json_str), lambda x: x)
q2 = (r.json(json_str).coerce_to('STRING'), json.loads)
q3 = (r.json(json_str).coerce_to('STRING'), ujson.loads)
def runq(q):
return q[1](q[0].run(c))
print runq(q1)
print runq(q2)
print runq(q3)
number = 10000
print "PROTOBUF", r.protobuf_implementation, ":", timeit(lambda: runq(q1), number=number)
print "JSON:", timeit(lambda: runq(q2), number=number)
print "UJSON:", timeit(lambda: runq(q3), number=number)
@AtnNn
Copy link
Author

AtnNn commented Nov 8, 2013

$ PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python python bench.py '[{"a":1,"b":2,"c":[1,2,3,4,5,6,7,"eight"]}]'
PROTOBUF python : 4.50813102722
JSON: 3.36550092697
UJSON: 3.19427013397
$ python bench.py '[{"a":1,"b":2,"c":[1,2,3,4,5,6,7,"eight"]}]'
PROTOBUF cpp : 2.72356700897
JSON: 1.9325799942
UJSON: 1.75751900673

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment