I wrote good code in python and it works great. But now I am increasing the size of the problems I am analyzing, and python is terribly slow. Slow piece of python code
for i in range(0,H,1): x1 = i - length x2 = i + length for j in range(0,W,1):
With H and W equal to 1024, the function takes about 5 minutes to fix. I wrote a simple C ++ program / function that performs the same calculation and produces in less than a second with the same data size.
double summ = 0; double total_num = 0; double tmp_num = 0 ; int avesize = 2; for( i = 0+avesize; i <X-avesize ;i++) for(j = 0+avesize;j<Y-avesize;j++) { // loop through sub region of the matrix // if the value is not zero add it to the sum // and increment the counter. for( int ii = -2; ii < 2; ii ++) { int iii = i + ii; for( int jj = -2; jj < 2 ; jj ++ ) { int jjj = j + jj; tmp_num = gsl_matrix_get(m,iii,jjj); if(tmp_num != 0 ) { summ = summ + tmp_num; total_num++; } } } gsl_matrix_set(Matrix_mean,i,j,summ/total_num); summ = 0; total_num = 0; }
I have several other methods for working with a 2D array. The listed list is a simple example.
What I want to do is pass the python 2D array to my C ++ function and return the 2D array back to python.
I read a little about swig and had serious questions, and it seems like this is a possible solution. But I canβt understand what I really need to do.
Can i get help? Thanks
c ++ python multidimensional-array swig
Jmd
source share