[Tutor] On learning Fortran and C++ for scientific computing

Laura Creighton lac at openend.se
Sun Apr 12 13:29:56 CEST 2015


In a message of Sun, 12 Apr 2015 10:25:54 +0400, "Vick" writes:
>S 0 to 1e+31....> 1/sqrt(.000086 *(1+z)^4 + .282 * (1+z)^3 - .000086
>*(1+z)^2   + .718)  if you try this integration you will get completely
>wrong numbers on computing devices that do not possess ultra-high precision
>and accuracy.

Python is as accurate as you can get, every bit as accurate as C++ or Fortran.

>I had to use mpmath with precision of 250 and integration method called Tanh
>Sinh Quadrature with accuracy 120 to get the desired results. I checked the
>answer with Mathematica and they concur. But the calculation is done in
>about 3 seconds on my laptop with intel core i5 at 2.5 GHz with 4 GB RAM.

>So can Fortran crunch 250 digits numbers in an integration formula under 3
>seconds with the same computing parameters as above? Or is Python better at
>it?

Some of us who were unhappy with the performance of python wrote pypy.
(I am one of the people, so very much biased here.)  PyPy uses a just-in-time
compiler to get its speed.  If you want to look at the speed of pypy vs the
speed of CPython (which is what we call the particular implementation(s)
of python you are using, because it is written in C) you can take a look
at these pages.  http://speed.pypy.org/

Click on a benchmark, and see the code, and see how much faster we are
than CPython.  Mathematical formulae are what we are very, very good
at.  Indeed we are often better than some old Fortran library -- so we
have users who are busy rewriting their code in Python in order to use
pypy for the increased speed of mathematical computation.  If all you
are doing is pure math -- and not going out, for instance and trying
to graph this thing at the same time -- then we can probably give you
speed which is equivalent to C.  But this assumes that you can write
this code where the heavy calculating is done inside a loop, which is
executed many times, because that is what PyPy optimises. If all you
have is straight in-line executed-one-time-only code, there will be no
speed win for you.

Also, the jit takes time to warm up.  It has to set up its own internal
bookkeeping before it can get to work on your code.  So if your code doesn't
take very long to run, it may run slower in pypy -- because the savings
your get are competely swallowed up in the setup costs.

However, it is easy to find out.  You can get pypy here:
http://pypy.org/download.html

It's a perfectly compliant, working python.  The version compatible with
3.2.5 is not as speedy as the one compatible with 2.7.  So just run your
python code with this -- it should run completely unchanged -- and see
how fast it is.

If you are still unhappy with the performance, post your code to
pypy-dev at python.org and we can give you suggestions on how to tune your
python code for better performance under pypy.  If you expose a place
where pypy ought to be fast, but isn't, we will fix pypy and get you
a working faster binary.

But I think any more talk about pypy belongs on the pypy-dev mailing list.

Laura Creighton


More information about the Tutor mailing list