Converting Python app to C++ completely

Isaac To kkto at csis.hku.hk
Fri Aug 30 10:18:14 EDT 2002


>>>>> "Alan" == Alan James Salmoni <salmonia.nospam.please at cardiff.ac.uk> writes:

    Alan> 1) How easy is it to convert existing Python code to C++? I read
    Alan> that each line of C++ code is roughly equivilent to 5-8 lines of
    Alan> C++ code, but is the conversion a simple task or is it
    Alan> troublesome?

That depends.  C++ and Python use completely different object model.  If
your original code depend on the object model, it is going to be much less
than trivial to switch.  E.g., if you depend on being able to manipulate an
object independent of its type as long as it has a certain function defined,
then you probably will not like the change to C++ (which will require both
types to inherit the same subclass, and then you have difficulty to cast it
back to the right kind of pointer to get back full functionality, etc.).  So
I'd say they are different, although I won't go so far as to argue which is
better.  My feeling is simply that at some time you have to make your design
right anyway, but Python give you the flexibility to delay actual design and
do things loosely, until you really think that a good design will buy you
something.  In C++, you can't code without a good design: things will be
much more difficult to write if your design is wrong (or sub-optimal).

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

Ignore them in C++.  In Python, most programmers know reduce and map pretty
well because they are critical to performance: an implicit for loop is
implemented in C, so it is much faster than an explicit for loop which
require generation of a lot of simple and stupid integer objects and then
invoke garbage collector.  C++ has no such burden: the compiler does the
same optimization whether you write your loop explicitly or write a template
function call.  Most people consider writing it explicitly more flexible and
easier to understand, so less than 10% of programmers I know ever remember
the C++ versions of reduce and map.  As a result, while in Python half of
the people think it is easier to understand/cleaner/don't object too much to
use map or reduce and half think it makes things more difficult to
understand but is more or less forced to do it anyway to achieve
performance, most C++ people find them very hard to understand.  The use of
such functionality in your program directly translates to a much reduced
maintainability of your program, since it is much harder to find the right
person to understand (and correct, in case there is a bug) such code.

My last advice: if you just want performance, try to find out where your
code is slow, and convert to C++ (or even plain C) only that part.  Use
module loading to load that part into Python so that that part runs in C++
speed.  This way you won't waste time designing where it won't buy you
something useful.  And, if you just want to learn things, learning the
dynamic loading interface will never let you down in satisfaction.

Regards,
Isaac.



More information about the Python-list mailing list