[Chandler-dev] performance of Python vs. SWIG'ed C++

Alec Flett alecf at osafoundation.org
Mon Mar 20 09:47:23 PST 2006


Hey John -
I was working on some of the color stuff, and I noticed you switched to 
using wx.Image_HSVtoRGB() instead of Python's built-in colorsys module.

I've tested this and the Python-based HSV/RGB conversion is actually 
significantly faster. I have no doubt that the raw C++ routines 
converting HSV to RGB are faster internally, but SWIG really slows 
things down. This was actually once of my "lessons learned" when doing 
calendar performance work: avoid calling through SWIG at all costs.

Here are the tests and results on my Windows box (Pentium M, 1.6Ghz) 
using our Visual Studio-based Python.

 > release/RunPython.bat -m timeit -s 'import wx;hue=120' 
'wx.Image.HSVtoRGB(wx.Image_HSVValue(hue/360.0, 0.5, 1.0))'
10000 loops, best of 3: *17.2 usec* per loop

 > release/RunPython.bat -m timeit -s 'from colorsys import 
hsv_to_rgb;hue=120' 'hsv_to_rgb(hue/360.0, 0.5, 1.0)'
100000 loops, best of 3: *3.55 usec* per loop

Almost 5 times faster! The only problem with using hsv_to_rgb is that 
the numbers come back in the 0 to 1.0 range, rather than 0..255. But 
that is easy to fix:

 > release/RunPython.bat -m timeit -s 'from colorsys import 
hsv_to_rgb;hue=120' 'rgb = [int(v) for v in hsv_to_rgb(hue/360.0, 0.5, 
1.0)]'
10000 loops, best of 3: *6.79 usec *per loop

So even with this conversion, it is more than 2.5x faster to use pure 
python than to keep calling through SWIG.

Alec
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osafoundation.org/pipermail/chandler-dev/attachments/20060320/839d31d6/attachment.htm


More information about the chandler-dev mailing list