Passing Python array in C ++ using SWIG

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): #print i, ',', j # check the limits y1 = j - length y2 = j + length IntRed[i,j] = np.mean(RawRed[x1:x2,y1:y2]) 

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

+6
c ++ python multidimensional-array swig
source share
1 answer

You can use arrays as described here: Doc - 5.4.5 Arrays , carray.i or std_vector.i from the SWIG library. I find it easier to work with std :: vector from the SWIG std_vector.i library to send a python list to the CIG C ++ extension. Although in your case, when optimization is important, this may not be optimal.

In your case, you can determine:

test.i

 %module test %{ #include "test.h" %} %include "std_vector.i" namespace std { %template(Line) vector < int >; %template(Array) vector < vector < int> >; } void print_array(std::vector< std::vector < int > > myarray); 

test.h

 #ifndef TEST_H__ #define TEST_H__ #include <stdio.h> #include <vector> void print_array(std::vector< std::vector < int > > myarray); #endif /* TEST_H__ */ 

test.cpp

 #include "test.h" void print_array(std::vector< std::vector < int > > myarray) { for (int i=0; i<2; i++) for (int j=0; j<2; j++) printf("[%d][%d] = [%d]\n", i, j, myarray[i][j]); } 

If you run the following python code (I used python 2.6.5), you can see that the C ++ function can access the python list:

 >>> import test >>> a = test.Array() >>> a = [[0, 1], [2, 3]] >>> test.print_array(a) [0][0] = [0] [0][1] = [1] [1][0] = [2] [1][1] = [3] 
+10
source share

All Articles