Converting Python app to C++ completely
Joe Knapka
jknapka at earthlink.nospam
Mon Sep 16 14:21:11 EDT 2002
Roy Smith 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 :-)
You might try rewriting it in Python, using lists of characters
instead of strings for the stuff that's causing performance
trouble. Given str, lst = list(str), then do all the processing
(in place) on lst, and ''.join(lst) to turn it back into a string.
Cheers,
-- Joe
More information about the Python-list
mailing list