[Numpy-discussion] ising model: f2py vs cython comparison
Ondrej Certik
ondrej@certik...
Sat Dec 22 19:29:07 CST 2007
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.
How to play with that - just do this (after installing Mercurial):
$ hg clone http://hg.sharesource.org/isingmodel/
[...]
$ hg up db7dd01cdc26 # just to be sure
that we are talking about the same revision / code
$ cd isingmodel
$ make
[...]
$ time python simulate.py
[...]
real 0m2.026s
user 0m1.988s
sys 0m0.020s
This runs Cython code. Then apply this patch to run fortran code instead:
$ hg di
diff -r db7dd01cdc26 simulate.py
--- a/simulate.py Sun Dec 23 02:23:30 2007 +0100
+++ b/simulate.py Sun Dec 23 02:24:33 2007 +0100
@@ -31,8 +31,8 @@ def MC(mu = 1, temp = 2, dim = 20, steps
J=1 #coupling constant
k=1 #Boltzman constant
- #from mcising import mc
- from pyising import mc
+ from mcising import mc
+ #from pyising import mc
B = D1(A)
mc(B, dim, steps, temp, H, mu, J, k)
return D2(B)
And then again:
$ time python simulate.py
[...]
real 0m3.600s
user 0m3.528s
sys 0m0.052s
So it's a lot slower.
Maybe I did some stupid mistake, like doing different amount of steps
in Cython and Fortran, but I tried to check that and if I did
everything all right, it's interesting to find why is fortran+f2py
slower.
Ondrej
More information about the Numpy-discussion
mailing list