-
Notifications
You must be signed in to change notification settings - Fork 365
/
benchmark2.py
64 lines (51 loc) · 1.9 KB
/
benchmark2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# coding=UTF-8
import simplejson
import ujson
import sys
try:
import json
except ImportError:
json = simplejson
import cjson
from time import time as gettime
import time
import sys
import random
def timeit_compat_fix(timeit):
if sys.version_info[:2] >= (2,6):
return
default_number = 1000000
default_repeat = 3
if sys.platform == "win32":
# On Windows, the best timer is time.clock()
default_timer = time.clock
else:
# On most other platforms the best timer is time.time()
default_timer = time.time
def repeat(stmt="pass", setup="pass", timer=default_timer,
repeat=default_repeat, number=default_number):
"""Convenience function to create Timer object and call repeat method."""
return timeit.Timer(stmt, setup, timer).repeat(repeat, number)
timeit.repeat = repeat
def ujsonDec():
x = ujson.loads(decodeData)
def simplejsonDec():
x = simplejson.loads(decodeData)
def ujsonEnc():
x = ujson.dumps(encodeData)
def simplejsonEnc():
x = simplejson.dumps(encodeData)
if __name__ == "__main__":
import timeit
timeit_compat_fix(timeit)
COUNT = 100
# Load file into memory
f = open("sample.json", "rb")
decodeData = f.read()
f.close()
encodeData = simplejson.loads(decodeData)
# Decode 1 million times
print "ujson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonDec()", "from __main__ import ujsonDec", gettime,10, COUNT)), )
print "simplejson decode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonDec()", "from __main__ import simplejsonDec", gettime,10, COUNT)), )
print "ujson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("ujsonEnc()", "from __main__ import ujsonEnc", gettime,10, COUNT)), )
print "simplejson encode : %.05f calls/sec" % (COUNT / min(timeit.repeat("simplejsonEnc()", "from __main__ import simplejsonEnc", gettime,10, COUNT)), )