Created
December 6, 2013 22:12
-
-
Save maemre/7833031 to your computer and use it in GitHub Desktop.
Python JSON encode/decode benchmark for short strings
This file contains hidden or 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
#!/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