This message was sent from Geocrawler.com by "andrew x swan" a.swan@anprod.csiro.au Be sure to reply to that address.
hi  i've only just started using python and numpy... the program i wrote below runs much more slowly than a fortran equivalent. ie. on a dataset where the order of the matrix is (3325,3325), python took this long:
362.25user 0.74system 6:09.78elapsed 98%CPU
and fortran took this long:
2.68user 1.12system 0:03.89elapsed 97%CPU
is this because the element by element calculations involved are contained in python for loops?
thanks
#!/usr/bin/python
from Numeric import *
def nrm(pedigree):
n_animals = len(pedigree) + 1
nrm = zeros((n_animals,n_animals),Float)
for i in xrange(1,n_animals): isire = pedigree[i1][1] idam = pedigree[i1][2] nrm[i,i] = 1.0 + 0.5 * nrm[isire,idam] for j in xrange(i+1,n_animals): jsire = pedigree[j1][1] jdam = pedigree[j1][2] nrm[j,i] = 0.5 * (nrm[jsire,i] + nrm[jdam,i]) nrm[i,j] = nrm[j,i]
return nrm
if __name__ == '__main__':
test_ped = [(1,0,0),(2,0,0),(3,1,0),(4,1,2), (5,3,4),(6,1,4),(7,5,6)]
a = nrm(test_ped) print a
Geocrawler.com  The Knowledge Archive
From: andrew x swan archiver@db.geocrawler.com
python took this long:
362.25user 0.74system 6:09.78elapsed 98%CPU
and fortran took this long:
2.68user 1.12system 0:03.89elapsed 97%CPU
is this because the element by element calculations involved are contained in python for loops?
yes.
david ascher
On Thu, 17 Feb 2000, andrew x swan wrote:
is this because the element by element calculations involved are contained in python for loops?
Hi Andrew!
I've only just begun using Numeric Python, but I'm a longtime user of GNU Octave and a sporadic user of MatLab. In general, for loops kill the execution speed of interpretive environments like Numpy and Octave.
The highspeed comes when one uses vector operations such as Matrix multiplication.
If you can vectorize your code, meaning replace all the loops with matrix operations, you should see equivalent speed to Fortran for large data sets. As far as I know, you will never see an interpreted language match a compiled one in the execution of for loops.
Thanks. Syrus.
participants (3)

andrew x swan

David Ascher

Syrus NematNasser