[Numpy-discussion] ising model: f2py vs cython comparison

Pearu Peterson pearu@cens.ioc...
Sun Dec 23 04:38:29 CST 2007


On Sun, December 23, 2007 3:29 am, Ondrej Certik wrote:
> Hi,
>
> I need to write 2D Ising model simulation into my school, so I wrote
> it in Python, then rewrote it in Fortran + f2py, and also Cython:
>
> http://hg.sharesource.org/isingmodel/
>
> And Cython solution is 2x faster than f2py. I understand, that I am
> comparing many things - wrappers, my fortran coding skills
> vs Cython C code generation and gcc vs gfortran. But still any ideas
> how to speed up fortran+f2py solution is very welcomed.

Though the problem is 2D, your implementations are essentially
1D. If you would treat the array A as 2D array (and avoid calling
subroutine p) then you would gain some 7% speed up in Fortran.

When using -DF2PY_REPORT_ATEXIT for f2py then a summary
of timings will be printed out about how much time was spent
in Fortran code and how much in the interface. In the given case
I get (nsteps=50000):

Overall time spent in ...
(a) wrapped (Fortran/C) functions           :     1962 msec
(b) f2py interface,               60 calls  :        0 msec
(c) call-back (Python) functions            :        0 msec
(d) f2py call-back interface,      0 calls  :        0 msec
(e) wrapped (Fortran/C) functions (acctual) :     1962 msec

that is, most of the time is spent in Fortran function and no time
in wrapper. The conclusion is that the cause of the
difference in timings is not in f2py or cpython generated
interfaces but in Fortran and C codes and/or compilers.

Some idiom used in Fortran code is just slower than in C..
For example, in C code you are doing calculations using
float precision but in Fortran you are forcing double precision.

HTH,
Pearu

PS: Here follows a setup.py file that I used to build the
extension modules instead of the Makefile:

#file: setup.py
def configuration(parent_package='',top_path=None):
    from numpy.distutils.misc_util import Configuration
    config = Configuration('',parent_package,top_path)
    config.add_extension('mcising', sources=['mcising.f'],
                         define_macros = [('F2PY_REPORT_ATEXIT',1)]
                         )
    #config.add_extension('pyising', sources=['pyising.pyx'])
    return config
from numpy.distutils.core import setup
setup(configuration = configuration)




More information about the Numpy-discussion mailing list