00001
00002 import simplejson
00003 import ujson
00004 import sys
00005 try:
00006 import json
00007 except ImportError:
00008 json = simplejson
00009 import cjson
00010 import yajl
00011 from time import time as gettime
00012 import time
00013 import sys
00014 import random
00015
00016
00017 user = { "userId": 3381293, "age": 213, "username": "johndoe", "fullname": u"John Doe the Second", "isAuthorized": True, "liked": 31231.31231202, "approval": 31.1471, "jobs": [ 1, 2 ], "currJob": None }
00018 friends = [ user, user, user, user, user, user, user, user ]
00019
00020 decodeData = ""
00021
00022 """=========================================================================="""
00023
00024 def ujsonEnc():
00025 x = ujson.encode(testObject, ensure_ascii=False)
00026
00027
00028 def simplejsonEnc():
00029 x = simplejson.dumps(testObject)
00030
00031
00032 def jsonEnc():
00033 x = json.dumps(testObject)
00034
00035
00036 def cjsonEnc():
00037 x = cjson.encode(testObject)
00038
00039
00040 def yajlEnc():
00041 x = yajl.dumps(testObject)
00042
00043
00044 """=========================================================================="""
00045
00046 def ujsonDec():
00047 x = ujson.decode(decodeData)
00048
00049
00050 def simplejsonDec():
00051 x = simplejson.loads(decodeData)
00052
00053
00054 def jsonDec():
00055 x = json.loads(decodeData)
00056
00057
00058 def cjsonDec():
00059 x = cjson.decode(decodeData)
00060
00061
00062 def yajlDec():
00063 x = yajl.loads(decodeData)
00064
00065
00066 """=========================================================================="""
00067
00068 def timeit_compat_fix(timeit):
00069 if sys.version_info[:2] >= (2,6):
00070 return
00071 default_number = 1000000
00072 default_repeat = 3
00073 if sys.platform == "win32":
00074
00075 default_timer = time.clock
00076 else:
00077
00078 default_timer = time.time
00079 def repeat(stmt="pass", setup="pass", timer=default_timer,
00080 repeat=default_repeat, number=default_number):
00081 """Convenience function to create Timer object and call repeat method."""
00082 return timeit.Timer(stmt, setup, timer).repeat(repeat, number)
00083 timeit.repeat = repeat
00084
00085
00086 if __name__ == "__main__":
00087 import timeit
00088 timeit_compat_fix(timeit)
00089
00090
00091 print "Ready? Configure affinity and priority, starting in 20..."
00092 time.sleep(20)
00093
00094 print "Array with 256 utf-8 strings:"
00095 testObject = []
00096
00097 for x in xrange(256):
00098 testObject.append("نظام الحكم سلطاني وراثي في الذكور من ذرية السيد تركي بن سعيد بن سلطان ويشترط فيمن يختار لولاية الحكم من بينهم ان يكون مسلما رشيدا عاقلا ًوابنا شرعيا لابوين عمانيين ")
00099
00100 COUNT = 2000
00101
00102
00103 print "ujson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonEnc()", "from __main__ import ujsonEnc", gettime,10, COUNT)), )
00104 print "simplejson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonEnc()", "from __main__ import simplejsonEnc", gettime,10, COUNT)), )
00105 print "cjson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("cjsonEnc()", "from __main__ import cjsonEnc", gettime, 10, COUNT)), )
00106 print "yajl encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("yajlEnc()", "from __main__ import yajlEnc", gettime, 10, COUNT)), )
00107
00108
00109
00110
00111 decodeData = json.dumps(testObject)
00112
00113 print "ujson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonDec()", "from __main__ import ujsonDec", gettime,10, COUNT)), )
00114 print "cjson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("cjsonDec()", "from __main__ import cjsonDec", gettime,10, COUNT)), )
00115 print "simplejson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonDec()", "from __main__ import simplejsonDec", gettime,10, COUNT)), )
00116 print "yajl decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("yajlDec()", "from __main__ import yajlDec", gettime,10, COUNT)), )
00117
00118 print "Medium complex object:"
00119 testObject = [ [user, friends], [user, friends], [user, friends], [user, friends], [user, friends], [user, friends]]
00120 COUNT = 5000
00121
00122 print "ujson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonEnc()", "from __main__ import ujsonEnc", gettime,10, COUNT)), )
00123 print "simplejson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonEnc()", "from __main__ import simplejsonEnc", gettime,10, COUNT)), )
00124 print "cjson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("cjsonEnc()", "from __main__ import cjsonEnc", gettime, 10, COUNT)), )
00125 print "yajl encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("yajlEnc()", "from __main__ import yajlEnc", gettime, 10, COUNT)), )
00126
00127 decodeData = json.dumps(testObject)
00128
00129 print "ujson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonDec()", "from __main__ import ujsonDec", gettime,10, COUNT)), )
00130 print "cjson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("cjsonDec()", "from __main__ import cjsonDec", gettime,10, COUNT)), )
00131 print "simplejson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonDec()", "from __main__ import simplejsonDec", gettime,10, COUNT)), )
00132 print "yajl decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("yajlDec()", "from __main__ import yajlDec", gettime,10, COUNT)), )
00133
00134 print "Array with 256 strings:"
00135 testObject = []
00136
00137 for x in xrange(256):
00138 testObject.append("A pretty long string which is in a list")
00139
00140 COUNT = 10000
00141
00142 print "ujson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonEnc()", "from __main__ import ujsonEnc", gettime,10, COUNT)), )
00143 print "simplejson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonEnc()", "from __main__ import simplejsonEnc", gettime,10, COUNT)), )
00144 print "cjson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("cjsonEnc()", "from __main__ import cjsonEnc", gettime, 10, COUNT)), )
00145 print "yajl encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("yajlEnc()", "from __main__ import yajlEnc", gettime, 10, COUNT)), )
00146
00147 decodeData = json.dumps(testObject)
00148
00149 print "ujson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonDec()", "from __main__ import ujsonDec", gettime,10, COUNT)), )
00150 print "cjson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("cjsonDec()", "from __main__ import cjsonDec", gettime,10, COUNT)), )
00151 print "simplejson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonDec()", "from __main__ import simplejsonDec", gettime,10, COUNT)), )
00152 print "yajl decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("yajlDec()", "from __main__ import yajlDec", gettime,10, COUNT)), )
00153
00154 print "Array with 256 doubles:"
00155 testObject = []
00156
00157 for x in xrange(256):
00158 testObject.append(sys.maxint * random.random())
00159
00160 COUNT = 10000
00161
00162 print "ujson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonEnc()", "from __main__ import ujsonEnc", gettime,10, COUNT)), )
00163 print "simplejson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonEnc()", "from __main__ import simplejsonEnc", gettime,10, COUNT)), )
00164 print "cjson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("cjsonEnc()", "from __main__ import cjsonEnc", gettime, 10, COUNT)), )
00165 print "yajl encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("yajlEnc()", "from __main__ import yajlEnc", gettime, 10, COUNT)), )
00166
00167 decodeData = json.dumps(testObject)
00168
00169 print "ujson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonDec()", "from __main__ import ujsonDec", gettime,10, COUNT)), )
00170 print "cjson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("cjsonDec()", "from __main__ import cjsonDec", gettime,10, COUNT)), )
00171 print "simplejson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonDec()", "from __main__ import simplejsonDec", gettime,10, COUNT)), )
00172 print "yajl decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("yajlDec()", "from __main__ import yajlDec", gettime,10, COUNT)), )
00173
00174 print "Array with 256 True values:"
00175 testObject = []
00176
00177 for x in xrange(256):
00178 testObject.append(True)
00179
00180 COUNT = 50000
00181
00182 print "ujson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonEnc()", "from __main__ import ujsonEnc", gettime,10, COUNT)), )
00183 print "simplejson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonEnc()", "from __main__ import simplejsonEnc", gettime,10, COUNT)), )
00184 print "cjson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("cjsonEnc()", "from __main__ import cjsonEnc", gettime, 10, COUNT)), )
00185 print "yajl encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("yajlEnc()", "from __main__ import yajlEnc", gettime, 10, COUNT)), )
00186
00187 decodeData = json.dumps(testObject)
00188
00189 print "ujson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonDec()", "from __main__ import ujsonDec", gettime,10, COUNT)), )
00190 print "cjson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("cjsonDec()", "from __main__ import cjsonDec", gettime,10, COUNT)), )
00191 print "simplejson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonDec()", "from __main__ import simplejsonDec", gettime,10, COUNT)), )
00192 print "yajl decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("yajlDec()", "from __main__ import yajlDec", gettime,10, COUNT)), )
00193
00194
00195 print "Array with 256 dict{string, int} pairs:"
00196 testObject = []
00197
00198 for x in xrange(256):
00199 testObject.append({str(random.random()*20): int(random.random()*1000000)})
00200
00201 COUNT = 5000
00202
00203 print "ujson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonEnc()", "from __main__ import ujsonEnc", gettime,10, COUNT)), )
00204 print "simplejson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonEnc()", "from __main__ import simplejsonEnc", gettime,10, COUNT)), )
00205 print "cjson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("cjsonEnc()", "from __main__ import cjsonEnc", gettime, 10, COUNT)), )
00206 print "yajl encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("yajlEnc()", "from __main__ import yajlEnc", gettime, 10, COUNT)), )
00207
00208 decodeData = json.dumps(testObject)
00209
00210 print "ujson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonDec()", "from __main__ import ujsonDec", gettime,10, COUNT)), )
00211 print "cjson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("cjsonDec()", "from __main__ import cjsonDec", gettime,10, COUNT)), )
00212 print "simplejson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonDec()", "from __main__ import simplejsonDec", gettime,10, COUNT)), )
00213 print "yajl decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("yajlDec()", "from __main__ import yajlDec", gettime,10, COUNT)), )
00214
00215 print "Dict with 256 arrays with 256 dict{string, int} pairs:"
00216 testObject = {}
00217
00218 for y in xrange(256):
00219 arrays = []
00220 for x in xrange(256):
00221 arrays.append({str(random.random()*20): int(random.random()*1000000)})
00222 testObject[str(random.random()*20)] = arrays
00223
00224 COUNT = 50
00225
00226 print "ujson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonEnc()", "from __main__ import ujsonEnc", gettime,10, COUNT)), )
00227 print "simplejson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonEnc()", "from __main__ import simplejsonEnc", gettime,10, COUNT)), )
00228 print "cjson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("cjsonEnc()", "from __main__ import cjsonEnc", gettime, 10, COUNT)), )
00229 print "yajl encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("yajlEnc()", "from __main__ import yajlEnc", gettime, 10, COUNT)), )
00230
00231 decodeData = json.dumps(testObject)
00232
00233 print "ujson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonDec()", "from __main__ import ujsonDec", gettime,10, COUNT)), )
00234 print "cjson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("cjsonDec()", "from __main__ import cjsonDec", gettime,10, COUNT)), )
00235 print "simplejson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonDec()", "from __main__ import simplejsonDec", gettime,10, COUNT)), )
00236 print "yajl decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("yajlDec()", "from __main__ import yajlDec", gettime,10, COUNT)), )
00237