Writing code to be optimizable

Chris Angelico rosuav at gmail.com
Wed Nov 23 01:45:07 EST 2011


On Wed, Nov 23, 2011 at 4:19 PM, snorble <snorble at hotmail.com> wrote:
> Sometimes I want to prototype a program in Python, with the idea of
> optimizing it later by rewriting parts of it in C or Cython. But I
> usually find that in order to rewrite the slow parts, I end up writing
> those parts very much like C or C++ anyway, and I end up wondering
> what is the point of using Python in such a project.

There's a few ways to prototype. If you use Python as the language of
your specs documents (or pseudo-Python, perhaps) - consider the entire
Python implementation as ephemeral, and use it to verify that your
specifications are correct, but not for any sort of performance - then
it doesn't matter that you've rewritten it completely, nor that you've
written C code in Python. You would have been writing C code in
English otherwise, anyway.

If you're not sure which parts you're prototyping (ie will rewrite in
C/C++) and which parts you're writing properly (ie will keep the
Python version as the production code), I'd still recommend using all
of Python's best features... but keep your function docstrings
comprehensive. When you rewrite it in C, you're able to take advantage
of what you learned first time around, but otherwise it's just
strictly reimplementing the docstring in a different environment.

Chris Angelico



More information about the Python-list mailing list