I want to see the performance between with and without C extension. I downloaded the latest 2.1.3 version. And make three builds using Python 2.6.6:
rm -rf build ; python setup.py build && rm build/lib*/simplejson/_speedups.so
rm -rf build ; python setup.py build
rm -rf build ; CFLAGS="-march=core2 -O2 -pipe -fomit-frame-pointer" python setup.py build
The first one removes the compiled C extension, the second one is normal, then third one uses the current CFLAGS
I use in emerge. And the following lines is how C extensions got compiled,
Without customized CFLAGS x86_64-pc-linux-gnu-gcc -pthread -fPIC -I/usr/include/python2.6 -c simplejson/_speedups.c -o build/temp.linux-x86_64-2.6/simplejson/_speedups.o x86_64-pc-linux-gnu-gcc -pthread -shared build/temp.linux-x86_64-2.6/simplejson/_speedups.o -L/usr/lib64 -lpython2.6 -o build/lib.linux-x86_64-2.6/simplejson/_speedups.so With customized CFLAGS x86_64-pc-linux-gnu-gcc -pthread -march=core2 -O2 -pipe -fomit-frame-pointer -fPIC -I/usr/include/python2.6 -c simplejson/_speedups.c -o build/temp.linux-x86_64-2.6/simplejson/_speedups.o x86_64-pc-linux-gnu-gcc -pthread -shared -march=core2 -O2 -pipe -fomit-frame-pointer build/temp.linux-x86_64-2.6/simplejson/_speedups.o -L/usr/lib64 -lpython2.6 -o build/lib.linux-x86_64-2.6/
I use the following code to do the test,
import timeit
t = timeit.Timer('json.loads(json_str)', 'import simplejson as json;json_str=open("test.json","r").read()')
print t.timeit(100)
The test.json
is http://googleblog.blogspot.com/feeds/posts/default?alt=json&max-results=500
, over 3 MB, 500 entires.
The results is
Elapsed time for 100 loads() Without C extension : 126.230s json 1.9 in Python 2.6.6 : 60.616s With C extension : 9.945s With C extension (CFLAGS) : 7.555s
With C extension is at least 10 times faster. I also put the simplejson 1.9 which in Python 2.6.6 in the result. Without extension, 1.9 -> 2.1.3, twice more slower. I didn't download simplejson 1.9 to double check, but I don't think it's modified for being shipped with Python.