Converting Python app to C++ completely

Magnus Lyckå magnus at thinkware.se
Mon Sep 16 09:17:31 EDT 2002


Alan James Salmoni wrote:
> I have written an application (a package for statistical analysis - 
> SalStat) using Python and wxPython, not just because I wanted to, but 
> also to learn the language. I found both Python & wxPython to be superb 
> for the task.

Not surprising ;)

> Now, what I want to do is write the thing in C++. The reason? heh, 
> because I want to ;) Well, actually I want to learn C++ too, and I would 
> like to have the performance of a compiled program for larger data sets 
> (just for fun and comparison more than anything).

<wink>
"Premature optimization is the root of all evil!" - Don Knuth
</wink>

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.

Writing extension modules is one solution, I'm sure you are
aware of the efforts of SciPy/weave and Pyrex etc as well.
But that's beside the point if the purpose is to learn C++.

For learning C++, I can heartily recommend Scott Meyers' books,
at least if you know some C. See
http://www.thinkware.se/cgi-bin/thinki.cgi/Effective_20C_2b_2b
www.amazon.com/exec/obidos/search-handle-url/index=books&field-author=Meyers%2C%20Scott 

> I know very little about C++, but have a few questions about converting 
> from Python code. I am asking here because Pythonistas are more likely 
> to know C++ than vice versa (and CLP is one of the friendliest groups 
> around - creep, creep).
> 
> 1) How easy is it to convert existing Python code to C++? I read that 
> each line of C++ code is roughly equivilent to 5-8 lines of C++ code, 
> but is the conversion a simple task or is it troublesome?

I programmed a lot in both languages, but I haven't really
done that. While I have glanced at your program, I haven't
really looked through the source. I guess the only true
answer is that it depends a lot on how your code is written.
It's certainly possible to write C++ and Python programs in a
very similar structure.

I imagine it's not so difficult to map wxPython to wxWindows,
and with most logic being arithmetic operations on floats or
integers, I imagine it won't be so bad. I guess you don't
depend so much on the Python Standard Libraries.

Considerig this, I think your C++ program will be less than
five times larger than the equivalent Python program. Maybe
factor three is more likely.

> 2) Are there any pitfalls to beware of - I understand that dealing with 
> memory and pointers might be a problem, but like I said earlier, I just 
> don't really know?

Certainly. C++ is full of pitfalls. I mean, C is well known for
all it's pitfalls, and while C++ fixes a few of those, it adds
a lot more complexity to the language.

Meyers' books are concerned with these issues.

> 3) Does C++ have equivilent functional constructs like reduce and map? 
> These helped to make my code much cleaner, tighter and faster, and I 
> would miss them dearly.

Sure. They are part of the Standard Library (previously know as STL)
of the new, Standard C++. Unfortunately it seems to me that learning
the details of the Standard Library is a comparable undertaking as
learning the details of the Python standard library. And the C++
Standard Library will only give you the functional equivalent of
the core Python language without any libraries at all (well, except
for maybe math, array, sys and struct). In other words, the bulk
of the C++ Standard Library only remedies the lack of richness in
the C++ syntax. It's nothing like the "batteries included" approach
of Python.

I'm probably exaggerating a bit, but the standard containers and
algorithms are really awkward in comparision with the straight
forward approach of Python.

> btw - the code I am writing doesn't rely upon any platform specific 
> stuff at all and is quite portable in Python - I will be aiming for the 
> same thing in Python and assume it will be possible.

I guess you mean that you aim for the same thing in C++.
I'm afraid Standard C++ is not yet so standard, so that might
not be the easiest thing...




More information about the Python-list mailing list