n-body problem at shootout.alioth.debian.org

Matteo mahall at ncsa.uiuc.edu
Fri Oct 6 22:32:09 CEST 2006

Peter Maas wrote:
> I have noticed that in the language shootout at shootout.alioth.debian.org
> the Python program for the n-body problem is about 50% slower than the Perl
> program. This is an unusual big difference. I tried to make the Python program
> faster but without success. Has anybody an explanation for the difference?
> It's pure math so I expected Perl and Python to have about the same speed.
> Peter Maas, Aachen

Well, one implementation difference is that the Perl script uses
parallel global arrays
(one array for each positional component, one for each velocity
component, and one for mass) whereas the python implementation uses
planet objects. Now, this may not actually make too much of a
difference as it is, but if you really want to make the python version
faster, I'd use the Numeric or Numpy (parts of which may soon be
standardized), and do array-wide computations, rather than using python
loops (at least replacing the innermost loops). My reasonably
well-informed guess is that the speed would improve markedly.

Of course, numpy is not a standard package (though there is a proposal
to add a standard 'array' package to python, based of numpy/numeric),
but if you want to do any numerics with python, you shouldn't be
without it.


More information about the Python-list mailing list