Question about scientific calculations in Python

Albert Hofkamp hat at se-46.wpa.wtb.tue.nl
Tue Mar 12 10:41:32 EST 2002


On Wed, 13 Mar 2002 01:22:18 +1300, Martin Kaufmann <martinkaufmann at yahoo.com> wrote:

>I'm trying to write a program to calculate diffraction patterns for
>electron diffraction on clusters (100-10'000 atoms). So far I used
>Python only for CGI programming and system tools (on a rather low
>level...). Therefore I don't know whether it's reasonable to program
>such a project in Python (I really like the language). Presently my
>man concern is speed as the main function of the program is a nested

In general, there are at least 2 steps in a problem:
- Figure out what to do (what should I do in what order to obtain correct
  results, how should I organize my software).
- Figure out how to do it fast enough.

Python is extremely well suited for the first step. It is high level, so
experimenting can be done fast, without digging for low-level coding errors
(for example, the amount of code needed to implement a dictionary correctly is
quite astonishing with ample opportunity to make hard-to-find errors).
Being high-level also gives you the benefit that you can concentrate on finding
a good algorithm. Use as much libraries of Python as possible, as they tend to
be optimized for their task (and it saves you from having to code it).

If you program well, and not use too many dynamic features of Python, you can
consider the code as a kind of prototype for an implementation in e.g. C.
Also, selecting a good algorithm has in general more impact on execution
speed than optimizing a bad algorithm.

'Fast enough' in the second step heavily depends on your expectations and/or
requirements. I always try it first, and if I think it should be calculated
faster, then I think about speed.
First of all, if you can break the computation in independant pieces, do so,
and use more than 1 computer. If that is not enough, see whether you can use
faster or different libraries. If that is not enough, then re-code the program
in C/C++ (which should be not too difficult, since you already have explored
the best way to implement the program using Python (i.e. you don't need to do
step 1 again).

>Now my questions: Would it be best to
>
>(a) write the whole program in C/C++ (I know only the basics of C but
>it would be a good "excuse" to learn it...)?

As a rule of the thumb I use that I need to save at least as much time running
the program, as I am spending on writing it.
Thus if by re-implementing, I save 1 hour execution time, and I run it 10
times, then I must be able to rewrite the code in 10 hours, otherwise I am
wasting time.
(if you want to learn a language, this rule doesn't always hold of course).


Albert
-- 
Constructing a computer program is like writing a painting



More information about the Python-list mailing list