The code
import numpy as np
import aravir as ar
import time
n = 1000
u = np.ones((n,n))
v = np.ones((n,n))
e = np.ones((n,n))
t = time.clock()
d = ar.add3(u,v)
tfortran= time.clock()-t
t = time.clock()
for i in range (n):
for j in range (n):
e[i,j] = u[i,j]+v[i,j]
tnative = time.clock()-t
print 'fortran ', d
print 'native', e
print 'tfortran = ', tfortran, ', tnative = ', tnative
The fortran module I imported to python
subroutine add3(a, b, c, n)
double precision a(n,n)
double precision b(n,n)
double precision c(n,n)
integer n
cf2py intent(in) :: a,b
cf2py intent(out) :: c,d
cf2py intent(hide) :: n
do 1700 i=1, n
do 1600 j=1, n
c(i,j) = a(i,j)
$ +b(i,j)
1600 continue
1700 continue
end
save it as aravir.f and compile using
$ f2py -c aravir.f -m aravir
And here the result
$ python cobamodul.py
fortran [[ 2. 2. 2. ..., 2. 2. 2.]
[ 2. 2. 2. ..., 2. 2. 2.]
[ 2. 2. 2. ..., 2. 2. 2.]
...,
[ 2. 2. 2. ..., 2. 2. 2.]
[ 2. 2. 2. ..., 2. 2. 2.]
[ 2. 2. 2. ..., 2. 2. 2.]]
native [[ 2. 2. 2. ..., 2. 2. 2.]
[ 2. 2. 2. ..., 2. 2. 2.]
[ 2. 2. 2. ..., 2. 2. 2.]
...,
[ 2. 2. 2. ..., 2. 2. 2.]
[ 2. 2. 2. ..., 2. 2. 2.]
[ 2. 2. 2. ..., 2. 2. 2.]]
tfortran = 0.069974 , tnative = 1.202547
The Desktop, :)
No comments:
Post a Comment