[Numpy-discussion] matrix multipln takes too much time
devnew@gmai...
devnew@gmai...
Tue Dec 25 08:47:29 CST 2007
hi
i am doing some maths calculations involving matrices of double values
using numpy.matrix ,
java code for this is something like
int items=25;
int sample=5;
int totalcols=8100;
double[][]dblarrayone=new double[items][totalcols];
double[][]dblarraytwo=new double[items][totalcols];
//their elements are set elsewhere before calculation
double[][] resultarray = new double[items][sample];
for(int i=0;i<items;i++){
for(int j=0;j<sample;j++){
double tval=0.0;
for(int p=0;p<totalcols;p++)
tval+=dblarrayone[j][p] * dblarraytwo[i][p];
resultarray[i][j]=Math.abs(tval);
}
}
so I wanted to do the same in python..(may be this code is not in the
recommended way..)
i am storing the filled matrices and other values as instance variable
of a class and access them by self.whatever...
self.items=25
self.sample=5
self.totalcols=8100
#self.matrixone,self.matrixtwo are numply matix objects with already
filled elements
#but for testing i filled it with zeros
self.matrixone=matrix(zeros((items,totalcols)))
self.matrixtwo=matrix(zeros((items,totalcols)))
resultmatrix=matrix(zeros((self.items,self.sample)))
for i in range(self.items):
for j in range(self.sample):
tval=0.0
for p in range(self.totalcols):
tval +=self.matrixone[ j , p ] * self.matrixtwo[ i , p ]
resultmatrix[ i, j ]=abs(tval)
here I found that while the java code takes barely 110 milliseconds to
execute the code ,the
python code takes something like 53 secs to execute !!..I am baffled
by this ..can anyone advise me how i can improve this? (i want to code
in python so I can't use c,c++ , java)
dn
More information about the Numpy-discussion
mailing list