Created
March 1, 2009 17:53
-
-
Save inklesspen/72413 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
import time | |
import simplejson | |
import cjson | |
import zlib | |
import string | |
import random | |
chars = string.ascii_letters + string.digits | |
def get_random_string(s): | |
return ''.join(random.sample(chars, s)) | |
def get_random_records(num=5000): | |
data = [] | |
for x in xrange(num): | |
data.append({ | |
'key': get_random_string(15), | |
'value': get_random_string(20), | |
'first': get_random_string(15), | |
'last': get_random_string(15), | |
'type': random.choice(["A", "CNAME"]), | |
'ttl': random.choice([86400,3600,60]), | |
}) | |
return data | |
def ser_json(): | |
j = simplejson.dumps(raw) | |
return len(j) | |
def ser_json_compressed(): | |
j = simplejson.dumps(raw) | |
return len(zlib.compress(j)) | |
def serde_json(): | |
j = simplejson.dumps(raw) | |
simplejson.loads(j) | |
def ser_cjson(): | |
j = cjson.encode(raw) | |
return len(j) | |
def ser_cjson_compressed(): | |
j = cjson.encode(raw) | |
return len(zlib.compress(j)) | |
def serde_cjson(): | |
j = cjson.encode(raw) | |
cjson.decode(j) | |
def t(f): | |
s = time.clock() | |
ret = f() | |
e = time.clock() | |
return e-s, ret | |
if __name__ == "__main__": | |
x, raw = t(get_random_records) | |
print len(raw), 'total records (%0.3fs)' % x | |
print 'ser_json (%0.3fs) %s bytes' % t(ser_json) | |
print 'ser_cjson (%0.3fs) %s bytes' % t(ser_cjson) | |
print 'ser_json_compressed (%0.3fs) %s bytes' % t(ser_json_compressed) | |
print 'ser_cjson_compressed (%0.3fs) %s bytes' % t(ser_cjson_compressed) | |
print 'serde_json (%0.3fs)' % t(serde_json)[0] | |
print 'serde_cjson (%0.3fs)' % t(serde_cjson)[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment