1

I've been using the json module in python 2.6 but is very slow. I'd like to use a faster implementation. I've seen cjson but seems the development is not coing on and the api is different from the json module. I have also read some comments about a way to speed up json.

Any ideas?

smci
  • 32,567
  • 20
  • 113
  • 146
Juanjo Conti
  • 28,823
  • 42
  • 111
  • 133
  • 2
    http://stackoverflow.com/questions/706101/python-json-decoding-performance – Brandon Frohbieter Mar 01 '11 at 18:35
  • Thanks. In my linux installation I have json. simplejson and _json. Only the lastone seems to be written in C. – Juanjo Conti Mar 01 '11 at 18:41
  • This was a known issue in 2.6 and has been fixed. This question is a duplicate of http://stackoverflow.com/questions/706101/python-json-decoding-performance as [Orbit](http://stackoverflow.com/users/369591/orbit) noted, and should be closed. See that question for lots of benchmark data. – smci Apr 26 '13 at 11:59

2 Answers2

7

The most recent versions of simplejson are considerably faster than the one built in to python 2.6, and have the same API. If you want your python code to continue working even when simplejson is not installed, try this:

try:
    import simplejson as json
except ImportError:
    import json

See also:

http://bugs.python.org/issue6013

http://bugs.python.org/issue7451

ʇsәɹoɈ
  • 22,757
  • 7
  • 55
  • 61
1

For encoding, you can use iterencode. It uses less memory for the string, so serializing very large data structures takes considerably less time.

nmichaels
  • 49,466
  • 12
  • 107
  • 135