Created
March 17, 2014 13:24
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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