Converting Python app to C++ completely

Roy Smith roy at panix.com
Mon Sep 16 15:56:34 CEST 2002


Magnus Lycka <magnus at thinkware.se> wrote:
> A common approach is to factor out the performance critical
> parts (once you have actually measured) and rewrite them in
> C or C++. The profiler is a good tool for this measuring.

I'm in the process of doing exactly that.  I've got an app I wrote in 
Python which needed speeding up.  Profiling showed that 99% of the CPU 
time was spent in one class, which did a lot of low-level character 
processing.  Mostly what it did was thrash around creating string 
objects and tearing them down again in a rather un-pythonic way.

Seemed like a perfect opportunity to learn C++, so I re-wrote the class 
in that language.  Somewhat surprisingly, I only got about a 2x speedup.  
I think the reason is because now instead of spending all my time 
allocating and deallocating Python strings, I'm doing the same with C++ 
strings :-)

I'm going to take a shot at re-writing it again using C-style arrays of 
characters.  It'll be interesting to see how much speedup I get.  I 
figure one of two things will happen; either the C version will be much 
faster than the C++ version, or it won't.  Either way, I figure I will 
have learned something about C++, and maybe about Python too :-)



More information about the Python-list mailing list