[Numpy-discussion] f2py performance question from a rookie

Robin robince at gmail.com
Mon Aug 16 06:05:37 EDT 2010


On Mon, Aug 16, 2010 at 10:12 AM, Vasileios Gkinis <v.gkinis at nbi.ku.dk>wrote:

>  Hi all,
>
> This is a question on f2py.
> I am using a Crank Nicholson scheme to model a diffusion process and in the
> quest of some extra speed I started playing with f2py.
> However I do not seem to be able to get any significant boost in the
> performance of the code.
>

Try adding
 -DF2PY_REPORT_ON_ARRAY_COPY
to the f2py command line.

This will cause f2py to report any array copies. If any of the
types/ordering of the arrays don't match f2py will silently make a copy -
this can really affect performance.

Cheers

Robin


>
> In the following simple example I am building a tridiagonal array with
> plain python for loops and by calling a simple fortran subroutine that does
> the same thing.
>
> Here goes the python script:
>
> import numpy as np
> import time
> import learnf90_05
> import sys
>
> run = sys.argv[1]
>
> try:
>     dim = np.float(sys.argv[2])
> except:
>     dim = 500
> elem = np.ones(dim)
>
> ### Python routine
>
> if run == "Python":
>     t_python_i = time.time()
>     array_python = np.zeros((dim, dim))
>     for row in np.arange(1,dim-1):
>         array_python[row, row-1] = elem[row]
>         array_python[row, row] = elem[row]
>         array_python[row,row+1] = elem[row]
>
>     t_python_f = time.time()
>     python_time = t_python_f - t_python_i
>     print("Python time: %0.5e" %(python_time))
>
> ###fortran routine
>
> elif run == "Fortran":
>     t_fortran_i = time.time()
>     fortran_array = learnf90_05.test(j = dim, element = elem)
>     t_fortran_f = time.time()
>     fortran_time = t_fortran_f - t_fortran_i
>     print("Fortran time: %0.5e" %(fortran_time))
>
> And the fortran subroutine called test is here:
>
> subroutine test(j, element, a)
> integer, intent(in) ::  j
> integer :: row, col
> real(kind = 8),  intent(in) :: element(j)
> real(kind = 8), intent(out) :: a(j,j)
>
> do row=2,j-1
>     a(row,row-1) = element(row)
>     a(row, row) = element(row)
>     a(row, row+1) = element(row)
> enddo
>
> end
>
> The subroutine is compiled with Gfortran 4.2 on Osx 10.5.8.
>
> Running the python script with array sizes 300x300 and 3000x3000 gives for
> the python plain for loops:
>
> dhcp103:learnf vasilis$ python fill_array.py Python 3000
> Python time: 1.56063e-01
> dhcp103:learnf vasilis$ python fill_array.py Python 300
> Python time: 4.82297e-03
>
> and for the fortran subroutine:
>
> dhcp103:learnf vasilis$ python fill_array.py Fortran 3000
> Fortran time: 1.16298e-01
> dhcp103:learnf vasilis$ python fill_array.py Fortran 300
> Fortran time: 8.83102e-04
>
> It looks like the gain in performance is rather low compared to tests i
> have seen elsewhere.
>
> Am I missing something here..?
>
> Cheers...Vasilis
>
>
>  --
>  ------------------------------------------------
> Vasileios Gkinis PhD student
>  Center for Ice and Climate <http://www.iceandclimate.nbi.ku.dk>
>  Niels Bohr Institute <http://www.nbi.ku.dk/english/>
> Juliane Maries Vej 30
> 2100 Copenhagen
> Denmark
>   email: v.gkinis at nbi.ku.dk <v.gkinis at nbi.ku.dk?>  skype: vasgkin  Tel: +45
> 353 25913  ------------------------------------------------
>  --
>  ------------------------------------------------
> Vasileios Gkinis PhD student
>  Center for Ice and Climate <http://www.iceandclimate.nbi.ku.dk>
>  Niels Bohr Institute <http://www.nbi.ku.dk/english/>
> Juliane Maries Vej 30
> 2100 Copenhagen
> Denmark
>   email: v.gkinis at nbi.ku.dk <v.gkinis at nbi.ku.dk?>  skype: vasgkin  Tel: +45
> 353 25913  ------------------------------------------------
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100816/65f20e61/attachment.html>


More information about the NumPy-Discussion mailing list