benchmark2.py
Go to the documentation of this file.
00001 # coding=UTF-8
00002 import simplejson
00003 import ujson
00004 import sys
00005 try:
00006     import json
00007 except ImportError:
00008     json = simplejson
00009 import cjson
00010 from time import time as gettime
00011 import time
00012 import sys
00013 import random
00014 
00015 def timeit_compat_fix(timeit):
00016     if sys.version_info[:2] >=  (2,6):
00017         return
00018     default_number = 1000000
00019     default_repeat = 3
00020     if sys.platform == "win32":
00021         # On Windows, the best timer is time.clock()
00022         default_timer = time.clock
00023     else:
00024         # On most other platforms the best timer is time.time()
00025         default_timer = time.time
00026     def repeat(stmt="pass", setup="pass", timer=default_timer,
00027        repeat=default_repeat, number=default_number):
00028         """Convenience function to create Timer object and call repeat method."""
00029         return timeit.Timer(stmt, setup, timer).repeat(repeat, number)
00030     timeit.repeat = repeat
00031 
00032 def ujsonDec():
00033     x = ujson.loads(decodeData)
00034 
00035 def simplejsonDec():
00036     x = simplejson.loads(decodeData)
00037     
00038 def ujsonEnc():
00039     x = ujson.dumps(encodeData)
00040 
00041 def simplejsonEnc():
00042     x = simplejson.dumps(encodeData)
00043         
00044     
00045 if __name__ == "__main__":
00046     import timeit
00047     timeit_compat_fix(timeit)
00048     
00049 COUNT = 100
00050 
00051 # Load file into memory
00052 f = open("sample.json", "rb")
00053 decodeData = f.read()
00054 f.close()
00055 
00056 encodeData = simplejson.loads(decodeData)
00057 
00058 # Decode 1 million times
00059 print "ujson decode      : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonDec()", "from __main__ import ujsonDec", gettime,10, COUNT)), )
00060 print "simplejson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonDec()", "from __main__ import simplejsonDec", gettime,10, COUNT)), )
00061 
00062 print "ujson encode      : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonEnc()", "from __main__ import ujsonEnc", gettime,10, COUNT)), )
00063 print "simplejson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonEnc()", "from __main__ import simplejsonEnc", gettime,10, COUNT)), )
00064 


rosbridge_library
Author(s): Jonathan Mace
autogenerated on Thu Jan 2 2014 11:53:35