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

Peter Otten __peter__ at web.de
Sun Apr 12 10:43:35 CEST 2015


Vick wrote:

> 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?

So by better you mean faster. 

Pure CPython is usually much slower than Fortran, but as there are many 
optimised libraries written in C or sometimes even Fortran available for use 
with CPython, that often doesn't matter.

The mpmath documentation states that the library's performance can be 
improved by using gmp, so in this case the library you should rely on is 
gmpy or gmpy2.

Do you have one of these installed?

What does

$ python3 -c 'import mpmath.libmp; print(mpmath.libmp.BACKEND)'
gmpy

print? 


Once you have your script working with gmp you could simplify it to just the 
integration, and when you are down to 10 or 20 lines you could challenge 
your friend to write the Fortran equivalent.

If he answers he's not willing to spend a week on the superior Fortran 
solution, or if he comes up with something that takes 2.7 seconds you see 
that he was bullshitting you. 

If his code finishes in 0.1 second and with the correct result, you might 
consider learning the language -- but keep in mind that to save one hour of 
execution time you have to run the script more than 1200 times, and when you 
need 10 minutes longer to write the Fortran than the Python code you may 
still get a bad deal as your time usually costs more than that of a machine.

That said, even though I rarely write any C code knowing C does help me 
understand what happens in C programs (for example the Python interpreter) 
and generally broadens the horizon. Therefore I'd recommend learning C (or 
Fortran) anyway if you are interested in the computing part of your 
research.



More information about the Tutor mailing list