Skip to content

Instantly share code, notes, and snippets.

@jhalcrow
Created April 2, 2011 14:23
Show Gist options
  • Save jhalcrow/899524 to your computer and use it in GitHub Desktop.
Save jhalcrow/899524 to your computer and use it in GitHub Desktop.
Benchmark of different serialization methods in Python, adapted from a J2 Labs Tumblr post
# Adapted from http://j2labs.tumblr.com/post/4262756632/speed-tests-for-json-and-cpickle-in-python
import time
import cPickle as pickle
import simplejson
import json
import cjson
import jsonlib
import ujson
cjson.dumps = cjson.encode
cjson.loads = cjson.decode
def test_mod(mod, iters=1000000):
d = {
'foo': 'bar',
'food': 'barf',
'good': 'bars',
'dood': 'wheres your car?',
'wheres your car': 'dude?',
}
print 'Starting %s...' % mod.__name__
start = time.time()
for i in xrange(iters):
s = mod.dumps(d)
write = time.time() - start
start = time.time()
for i in xrange(iters):
dl = mod.loads(s)
read = time.time() - start
print 'Read: %f, Write: %f, Total %f' % (read, write, read + write)
to_test = [cjson, ujson, json, simplejson, pickle, jsonlib]
for mod in to_test:
test_mod(mod)
@jhalcrow
Copy link
Author

jhalcrow commented Apr 2, 2011

My results:

Starting cjson...
Read: 2.172015, Write: 2.814428, Total 4.986443
Starting ujson...
Read: 0.943295, Write: 0.872316, Total 1.815611
Starting json...
Read: 10.960912, Write: 7.820410, Total 18.781322
Starting simplejson...
Read: 4.783837, Write: 9.053143, Total 13.836980
Starting cPickle...
Read: 4.127432, Write: 8.274755, Total 12.402187
Starting jsonlib...
Read: 11.151062, Write: 5.299074, Total 16.450136

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