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

John Travers jtravs@gmail....
Mon Dec 24 05:50:29 CST 2007

On Dec 23, 2007 12:57 PM, Ondrej Certik <ondrej@certik.cz> wrote:
> Hi,
> I needed to write 2D Ising model simulation into my school and I
> decided to compare the two possible solutions how to do it, so I of
> course wrote
> it in Python, then rewrote it in Fortran + f2py, and also Cython. What
> is better? Read below. :)  But for the impatient, I am going to use
> Cython, reasons below.

I recently tried a similar experiment with the opposite result :)

> So it's a lot slower.

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

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

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

Seasons greetings,

Telephone: (+44) (0) 7739 105209

More information about the Numpy-discussion mailing list