[Numpy-discussion] matrix multipln takes too much time

devnew@gmai... devnew@gmai...
Tue Dec 25 08:47:29 CST 2007

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];



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.matrixone,self.matrixtwo are numply matix objects with already
filled elements
#but for testing i filled it with zeros

for i in range(self.items):
    for j in range(self.sample):
         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)


More information about the Numpy-discussion mailing list