import timeit
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
testobj1 = ['JSON Test Pattern pass1',
{'object with 1 member': ['array with 1 element']},
{},
[],
-42,
True,
False,
None,
{'': 2.3456789012000001e+76,
' s p a c e d ': [1, 2, 3, 4, 5, 6, 7],
'# -- --> */': ' ',
'0123456789': 'digit',
'ALPHA': 'ABCDEFGHIJKLMNOPQRSTUVWYZ',
'E': 1.23456789e+34,
u'\\/\\"\ucafe\ubabe\uab98\ufcde\ubcda\uef4a\x08\x0c\n\r\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?': 'A key can be any string',
'address': '50 St. James Street',
'alpha': 'abcdefghijklmnopqrstuvwyz',
'array': [],
'backslash': '\\',
'comment': '// /* <!-- --',
'compact': [1, 2, 3, 4, 5, 6, 7],
'controls': '\x08\x0c\n\r\t',
'digit': '0123456789',
'e': 1.23456789e-13,
'false': False,
'hex': u'\u0123\u4567\u89ab\ucdef\uabcd\uef4a',
'integer': 1234567890,
'jsontext': '{"object with 1 member":["array with 1 element"]}',
'null': None,
'object': {},
'one': 1,
'quote': '"',
'quotes': u'" " %22 0x22 034 "',
'real': -9876.5432099999998,
'slash': '/ & \\/',
'space': ' ',
'special': "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
'true': True,
'url': 'http://www.JSON.org/',
'zero': 0},
0.5,
98.599999999999994,
99.439999999999998,
1066,
10.0,
1.0,
0.10000000000000001,
1.0,
2.0,
2.0,
'rosebud']
testobj2 = {
'name': u'blah blah',
'address': u'\u53ea'}
def compare(testobj, reps=10000):
cjson_setup = '\n'.join(['import cjson', 'testobj = ' + repr(testobj)])
simplejson_setup = '\n'.join(['import json', 'testobj = ' + repr(testobj)])
# make sure syntax is valid, before running either one
exec cjson_setup
exec simplejson_setup
cjson_t = timeit.Timer('cjson.encode(testobj)', cjson_setup)
print 'cjson=', cjson_t.timeit(reps)
# simplejson_t = timeit.Timer('json.dumps(testobj)', simplejson_setup)
# print 'simplejson=', simplejson_t.timeit(reps)
compare(testobj1)
compare(testobj2, 100000)