Skip to content

Instantly share code, notes, and snippets.

@steinnes
Created March 17, 2014 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steinnes/9599112 to your computer and use it in GitHub Desktop.
Save steinnes/9599112 to your computer and use it in GitHub Desktop.
Small benchmark script to time different serialization methods as we were evaluating json vs. simplejson vs. ujson vs. msgpack.
import json
import simplejson
import ujson
import msgpack
import time
class Timer(object):
def __init__(self, name):
self.name = name
def __enter__(self):
self.start_time = time.time()
def __exit__(self, exc_type, exc_value, traceback):
print "{}: {}s".format(self.name, time.time() - self.start_time)
def load(fn):
return json.loads(open(fn).read())
if __name__ == "__main__":
import sys
tree = load("latest.json")
if len(sys.argv) > 1:
tree = load(sys.argv[1])
print "Timing 10k encodes"
for lib in [json, simplejson, ujson, msgpack]:
with Timer(str(lib.__name__)):
for n in range(10000):
lib.dumps(tree)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment