[Numpy-discussion] gfortran/g77+f2py vs gcc+Cython speed comparison

Ondrej Certik ondrej@certik...
Mon Dec 24 07:29:00 CST 2007

Hi John,

thanks for the response.

> This is very surprising indeed. I have just tried to run your code but
> can't get the fortran generated module to work. But I wonder how you

Yes, I really didn't try to make it robust, I just made it work for me
on Debian unstable.

> got such a difference, because in my recent comparisons I generally
> got about 0-2 % faster (i.e. negligible) results with gfortran
> compared to gcc and a much bigger difference (in favour of fortran)
> with the Intel compilers.
> In fact for simple tests gcc and gfortran produce almost identical
> assembler code (use -s switch to check). For the more mature compilers
> (i.e. Intel) fortran is faster as it is inherently easier to optimize
> (easier data dependency analysis and vectorisation).
> The main reason I can think of is the gfortran version you are using
> and the optimisation switches.
> Any version 4.0.x is seriously suboptimal (first released version).
> Big gains were made from 4.0-4.1 and 4.1-4.2. I would suggest 4.2 as
> the minimum version to use. The development version 4.3 is faster
> still (also for gcc) because of recent vectorisation work.

I use:

$ gfortran --version
GNU Fortran (GCC) 4.2.3 20071123 (prerelease) (Debian 4.2.2-4)
Copyright (C) 2007 Free Software Foundation, Inc.

GNU Fortran comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of GNU Fortran
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING


> > When using g77 compiler instead of gfortran, I get a speed
> > up 4.8 times.
> The difference between g77 and gfortran with version 4.3 is now very
> small, plus gfortran is going to benefit from all the new
> vectorisation and optimisation work in gcc. It will not be long before
> it over takes for good. In addition you can use modern fortran 95/2003
> code which makes life much easier! In in addition to all that it is
> very actively maintained and destined to be the default compiler on
> all linux systems as it is bundled with gcc.

Yes, I also think gfortran is the way to go, since g77 is dead upstream.
But currently, the g77 still gives quite a lot faster code than gfortran,
but I hope it will change.

> > So, what do you think of that?
> >
> If you are using a modern gfortran version then I suggest you have
> found a bug, in which case I would take it to the gfortran mailing
> list. If not, then try version 4.3 which can be downloaded from the
> gfortran wiki: gcc.gnu.org/wiki/GFortran
> In the mean time, after Christmas, I'll try and get your code working
> and see for myself the difference.

So your suggestion is to prepare a clean fortran example, that,
when compiled with gfortran, runs a lot slower, than if compiled with g77?
Yep, I can do that.

So what is your personal opinion about fortran vs C for scientific computing?
>From the social point of view, I much prefer C, since a lot more people
know it, but as you say, the good fortran compilers are a lot better, than
the C compilers. So I don't know. Probably depends on the project and
a code I want to reuse.


More information about the Numpy-discussion mailing list