Python and JSON performance

07 Apr 2008

Ola. So I was working on a project and noticed that json encoding/decoding of a string to string dictionary was taking... uhhh 70% of the time. The are a few other benchmarks out there (kbyanc, hill-street), but none including the new simplejson 1.8.1 which has extensions to improve performance (links below).

My Tests

I'm only serializing dictionary mapping strings to strings. Maybe occasionally dict within a dict, but always primitive types. I don't need special ways of serializing custom objects. This test is just raw primitive types and data structure performance.

For this test, I made two dictionary with a dozen entries. One has both keys and value as ascii regular python strings, the other had the same data but used unicode strings.

cjson 1.0.6

cjson 1.0.6, which has the bug fix described here and the original source and homepage is over here. It's bit confusing, but it's worth it:

Serialization, cjson, ascii: 94
Serialization, cjson, unicode: 117
Deserialization, cjson, ascii: 80

simplejson 1.8.1

Available here The author maintains a blog.

This version has c-extensions for both encode and decode

Serialization, simplejson, ascii: 549
Serialization, simplejson, unicode: 592
Deserialization, simplejson, ascii: 2068

simplejson 1.6

This version is 100% pure python. It can be grabbed here

Serialization, simplejson, ascii: 2150
Serialization, simplejson, unicode: 1744
Deserialization, simplejson, ascii: 3318

Conclusion

Well, guess which version I'll be using. simplejson has all sorts of other features that you might want. Sometimes speed isn't everything, but for my application it is. Enjoy!