Fredrik Lundh is sharing code with you

Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.

Don't show this again

effbot / cjson-effbot

Various cjson tweaks, including byte string encoding support.

Clone this repository (size: 34.3 KB): HTTPS / SSH
hg clone https://bitbucket.org/effbot/cjson-effbot
hg clone ssh://hg@bitbucket.org/effbot/cjson-effbot

cjson-effbot / jsonbench.py

Branch
default
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'&#34; " %22 0x22 034 &#x22;',
  '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)