Converting Python app to C++ completely

Bengt Richter bokr at oz.net
Mon Sep 16 13:58:10 EDT 2002


On Mon, 16 Sep 2002 09:56:34 -0400, Roy Smith <roy at panix.com> wrote:

>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 :-)

Maybe you could post the culprit class and see what alternative _algorithms_
people come up with. It sounds like the badness is in your methods ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list