Skip to content

Instantly share code, notes, and snippets.

@maemre
Created December 6, 2013 22:12
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 maemre/7833031 to your computer and use it in GitHub Desktop.
Save maemre/7833031 to your computer and use it in GitHub Desktop.
Python JSON encode/decode benchmark for short strings
#!/usr/bin/env python
import time as t
import ujson as u
import simplejson as s
import json as j
N = 100000
x = [{"asderdgaveargeraf": {"asdregegf":3}, "jsdsafsdafkfds":[3, 4], 4:"sadkjsadfekjsdfkljsdfbkdjsf"}] * 2
z = [''] * N
libs = [
(u, "UltraJSON"),
(s, "SimpleJSON"),
(j, "Built-in JSON"),
]
print "Encode:"
print "----------------\n"
for lib in libs:
l, name = lib
t0 = t.clock()
for i in xrange(N):
z[i] = l.dumps(x)
t1 = t.clock()
print "%s\t%.6f s, %r calls/second" % (name, t1-t0, N * (t1 - t0) ** (-1))
print "\nDecode:"
print "----------------\n"
for lib in libs:
l, name = lib
t0 = t.clock()
for e in z:
l.loads(e)
t1 = t.clock()
print "%s\t%.6f s, %r calls/second" % (name, t1-t0, N * (t1 - t0) ** (-1))
print "\nEncode + Decode:"
print "----------------\n"
for lib in libs:
l, name = lib
t0 = t.clock()
for i in xrange(N):
l.loads(l.dumps(x))
t1 = t.clock()
print "%s\t%.6f s, %r calls/second" % (name, t1-t0, N * (t1 - t0) ** (-1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment