Gabor Kalman
2013-02-23 17:29:02 UTC
Im 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 buried the computation in a GFORTRAN sub, it required only 0.015 sec.
I cant find anything wrong with my source codes. So are these results plausible?
2. Codes:
module: py_v_p2fy.py
import time
import lib1
#====================== PYTHON ONLY =======================
def main1(z):
for i in range(10000):
for j in range(10000):
zz=2*z
start_time = time.clock()
z=2*3*4
main1(z)
print "done with PYTHON ONLY"
print time.clock() - start_time, "seconds"
print "-----"
#=================== PYTHON WITH F2PY ====================
def main2(x1,x2,x3):
lib1.f1(x1,x2,x3)
start_time = time.clock()
x1=2
x2=3
x3=4
main2(x1,x2,x3)
print "done with PYTHON WITH F2PY"
print time.clock() - start_time, "seconds"
#========
module: lib1.f90
subroutine f1(x1,x2,x3)
integer,intent(IN) :: x1,x2,x3
integer :: z,zz,i,j
do i=1,10000
do j=1,10000
z = x1*x2*x3
zz=2*z
end do
end do
end subroutine f1
#===================
module (batch-file): run.bat
python C:\Python27\Scripts\f2py.py ^
--build-dir .\tmp ^
--fcompiler=gnu95 ^
-c lib1.f90 -m lib1
pause
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 buried the computation in a GFORTRAN sub, it required only 0.015 sec.
I cant find anything wrong with my source codes. So are these results plausible?
2. Codes:
module: py_v_p2fy.py
import time
import lib1
#====================== PYTHON ONLY =======================
def main1(z):
for i in range(10000):
for j in range(10000):
zz=2*z
start_time = time.clock()
z=2*3*4
main1(z)
print "done with PYTHON ONLY"
print time.clock() - start_time, "seconds"
print "-----"
#=================== PYTHON WITH F2PY ====================
def main2(x1,x2,x3):
lib1.f1(x1,x2,x3)
start_time = time.clock()
x1=2
x2=3
x3=4
main2(x1,x2,x3)
print "done with PYTHON WITH F2PY"
print time.clock() - start_time, "seconds"
#========
module: lib1.f90
subroutine f1(x1,x2,x3)
integer,intent(IN) :: x1,x2,x3
integer :: z,zz,i,j
do i=1,10000
do j=1,10000
z = x1*x2*x3
zz=2*z
end do
end do
end subroutine f1
#===================
module (batch-file): run.bat
python C:\Python27\Scripts\f2py.py ^
--build-dir .\tmp ^
--fcompiler=gnu95 ^
-c lib1.f90 -m lib1
pause