Re: [f2py] f2py speed
Ethan Gutmann <[email protected]> Sat, 23 Feb 2013 13:25:28 -0700
| Newsgroups | gmane.comp.python.f2py.user |
|---|---|
| Message-ID | <5281300515267193342@unknownmsgid> |
--===============6409833435849009518== Content-Type: multipart/alternative; boundary=20cf3074b456e0159c04d66a1bda --20cf3074b456e0159c04d66a1bda Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable I'm not sure if it is happening in this case, but because you don't use the results in the fortran example, it is entirely possibly that the compiler is optimizing out the entire content of that subroutine. Benchmarks can be tricky to write for this sort of reason, so you really should try benchmarking your actual code if possible, or looking at other benchmarks around the web. Furthermore, if I were going to do something like that in python I would use numpy instead, for something this simple, numpy is likely to be within a factor of 2-10 of pure fortran code. Ethan On Feb 23, 2013, at 10:29 AM, Gabor Kalman <[email protected]> wrote: I=92m a relatively new user of f2py. To test what performance advantage I can get with f2py, I have created (a somewhat artificially) simple script. First I describe it and then I will show the source code. 1. Description: Take 3 constants (3 integers) and multiply those together in a double loop of range of 10,000 (i.e. 10**8 computations). If I use a PYTHON ONLY scrip, it took 28 sec. (with Windows 7 on a Toshiba C655D, Python 27) If I =93buried=94 the computation in a GFORTRAN sub, it required only 0.015= sec. I can=92t find anything wrong with my source codes. So are these results plausible? 2. Codes: module: py_v_p2fy.py import time import lib1 #=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D PYTHON = ONLY =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D def main1(z): for i in range(10000): for j in range(10000): zz=3D2*z start_time =3D time.clock() z=3D2*3*4 main1(z) print "done with PYTHON ONLY" print time.clock() - start_time, "seconds" print "-----" #=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D PYTHON WITH F2PY= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D def main2(x1,x2,x3): lib1.f1(x1,x2,x3) start_time =3D time.clock() x1=3D2 x2=3D3 x3=3D4 main2(x1,x2,x3) print "done with PYTHON WITH F2PY" print time.clock() - start_time, "seconds" #=3D=3D=3D=3D=3D=3D=3D=3D module: lib1.f90 subroutine f1(x1,x2,x3) integer,intent(IN) :: x1,x2,x3 integer :: z,zz,i,j do i=3D1,10000 do j=3D1,10000 z =3D x1*x2*x3 zz=3D2*z end do end do end subroutine f1 #=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D module (batch-file): run.bat python C:\Python27\Scripts\f2py.py ^ --build-dir .\tmp ^ --fcompiler=3Dgnu95 ^ -c lib1.f90 -m lib1 pause _______________________________________________ f2py-users mailing list f2py-users-Y4l6ocDipWCuvFJfX82//[email protected] http://cens.ioc.ee/mailman/listinfo/f2py-users --20cf3074b456e0159c04d66a1bda Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: quoted-printable <html><head><meta http-equiv=3D"content-type" content=3D"text/html; charset= =3Dutf-8"></head><body dir=3D"auto"><div>I'm not sure if it is happenin= g in this case, but because you don't use the results in the fortran ex= ample, it is entirely possibly that the compiler is optimizing out the enti= re content of that subroutine.=A0</div> <div><br></div><div>Benchmarks can be tricky to write for this sort of reas= on, so you really should try benchmarking your actual code if possible, or = looking at other benchmarks around the web.=A0</div><div><br></div><div>Fur= thermore, if I were going to do something like that in python I would use n= umpy instead, for something this simple, numpy is likely to be within a fac= tor of 2-10 of pure fortran code.=A0</div> <div><br></div><div>Ethan</div><div><br>On Feb 23, 2013, at 10:29 AM, Gabor= Kalman <<a href=3D"mailto:[email protected]">[email protected]</a= >> wrote:<br><br></div><blockquote type=3D"cite"><div> <div dir=3D"ltr"> <div style=3D"FONT-FAMILY:'Calibri';COLOR:#000000;FONT-SIZE:12pt"> <div>I=92m a relatively new user of f2py. </div> <div>To test what performance advantage I can get with f2py, I have created= (a=20 somewhat artificially) simple script.</div> <div>First I describe it and then I will show the source code.</div> <div>=A0</div> <div>1. Description:</div> <div>=A0</div> <div>Take 3 constants (3 integers) and multiply those together in a double = loop=20 of range=A0 of 10,000 (i.e. 10**8 computations).</div> <div>If I use a PYTHON ONLY scrip, it took 28 sec. (with Windows 7 on a Tos= hiba=20 C655D, Python 27)</div> <div>If I =93buried=94 the computation in a GFORTRAN sub, it required only = 0.015=20 sec.</div> <div>=A0</div> <div>I can=92t find anything wrong with my source codes. So are these resul= ts=20 plausible?</div> <div>=A0</div> <div>2. Codes:</div> <div>=A0</div> <div>module: py_v_p2fy.py</div> <div>=A0</div> <div>import time</div> <div>import lib1</div> <div>#=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D PY= THON ONLY =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D</div> <div>def main1(z):</div> <div>=A0=A0=A0 for i in range(10000):</div> <div>=A0=A0=A0=A0=A0=A0=A0 for j in range(10000):</div> <div>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=20 zz=3D2*z=A0=A0=A0 </div> <div>=A0</div> <div>start_time =3D time.clock()</div> <div>z=3D2*3*4</div> <div>main1(z)</div> <div>print "done with PYTHON ONLY"</div> <div>print time.clock() - start_time, "seconds"</div> <div>print "-----"</div> <div>#=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D PYTHON WITH= F2PY =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D</div> <div>def main2(x1,x2,x3):</div> <div>=A0=A0=A0 lib1.f1(x1,x2,x3)</div> <div>=A0</div> <div>start_time =3D time.clock()</div> <div>x1=3D2</div> <div>x2=3D3</div> <div>x3=3D4</div> <div>main2(x1,x2,x3)</div> <div>print "done with PYTHON WITH F2PY"</div> <div>print time.clock() - start_time, "seconds"</div> <div>=A0</div> <div>#=3D=3D=3D=3D=3D=3D=3D=3D</div> <div>module: lib1.f90</div> <div>=A0</div> <div>subroutine f1(x1,x2,x3)</div> <div>=A0=A0=A0 integer,intent(IN) :: x1,x2,x3</div> <div>=A0</div> <div>=A0=A0=A0 integer :: z,zz,i,j</div> <div>=A0</div> <div>=A0=A0=A0 do i=3D1,10000</div> <div>=A0=A0=A0=A0=A0=A0=A0 do j=3D1,10000</div> <div>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 z =3D=20 x1*x2*x3</div> <div>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=20 zz=3D2*z</div> <div>=A0=A0=A0=A0=A0=A0=A0 end do</div> <div>=A0=A0=A0 end do</div> <div>end subroutine f1</div> <div>=A0</div> <div>#=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D</div> <div>module (batch-file): run.bat</div> <div>=A0</div> <div>=A0</div> <div>python C:\Python27\Scripts\f2py.py ^</div> <div>=A0=A0=A0=A0 --build-dir .\tmp=A0 ^</div> <div>=A0=A0=A0=A0 --fcompiler=3Dgnu95=A0 ^</div> <div>=A0=A0=A0=A0 -c lib1.f90 -m lib1</div> <div>pause</div> <div>=A0</div> <div>=A0</div></div></div> </div></blockquote><blockquote type=3D"cite"><div><span>___________________= ____________________________</span><br><span>f2py-users mailing list</span>= <br><span><a href=3D"mailto:f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]">f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]<= /a></span><br> <span><a href=3D"http://cens.ioc.ee/mailman/listinfo/f2py-users">http://cen= s.ioc.ee/mailman/listinfo/f2py-users</a></span><br></div></blockquote></bod= y></html> --20cf3074b456e0159c04d66a1bda-- --===============6409833435849009518== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ f2py-users mailing list f2py-users-Y4l6ocDipWCuvFJfX82//[email protected] http://cens.ioc.ee/mailman/listinfo/f2py-users --===============6409833435849009518==--