Skip to content

Instantly share code, notes, and snippets.

@lxyu
Created November 22, 2012 09:00
Show Gist options
  • Save lxyu/4130091 to your computer and use it in GitHub Desktop.
Save lxyu/4130091 to your computer and use it in GitHub Desktop.
Python serialization benchmark
$ python benchmark.py
msgpack benchmark
7.78079032898
9.00101661682

ujson benchmark
12.1378898621
14.5480632782

tnetstring benchmark
22.6039886475
18.1319713593

json benchmark
32.399892807
43.4839725494

cPickle benchmark
60.6751441956
67.7280426025
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import msgpack
import json
import ujson
import tnetstring
import cPickle
from time import time
data = current = {}
for i in range(200):
current["big list"] = range(1000)
current["big string"] = "a" * 1000
current['child'] = {}
current = current['child']
print "msgpack benchmark"
start = time()
msg = msgpack.packb(data)
end = time()
print (end - start) * 1000
start = time()
msgpack.unpackb(msg)
end = time()
print (end - start) * 1000
print "ujson benchmark"
start = time()
msg = ujson.dumps(data)
end = time()
print (end - start) * 1000
start = time()
ujson.loads(msg)
end = time()
print (end - start) * 1000
print "tnetstring benchmark"
start = time()
msg = tnetstring.dumps(data)
end = time()
print (end - start) * 1000
start = time()
tnetstring.loads(msg)
end = time()
print (end - start) * 1000
print "json benchmark"
start = time()
msg = json.dumps(data)
end = time()
print (end - start) * 1000
start = time()
json.loads(msg)
end = time()
print (end - start) * 1000
print "cPickle benchmark"
start = time()
msg = cPickle.dumps(data)
end = time()
print (end - start) * 1000
start = time()
cPickle.loads(msg)
end = time()
print (end - start) * 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment