Using Python objects in C++ -


i'm writing code calculates images of nonlinear maps using methods interval analysis, applies minkowski sum , repeats arbitrary number of iterations.

i've written working code in python, able implement of more iteration/recursion intensive parts of algorithm in c++ benefit increased speed. have used cython in past great results, i'd practice c++.

also, objects complicated enough i'd rather avoid having implement them in c++ (baby steps!).

so questions are:

1) using python objects in c++ prevent improvement in efficiency?

2) if not, possible use cython wrap c++ function iterates/recurses on python object?

to more specific, have recursive algorithm recurses on left , right children of bst (though it's heavily modified bst i'd rather not bogged down details of implementing in c++), runtime quite prohibitive, i'd write in c++.

yes. speed-up on pure python not on par increase you'd if using pure c/c++. if want handle python objects you'll need via python c/api; adds overhead execution, price must pay being allowed interact python.

do note involves lot of complexity since need familiar api , read functions object references, how create lists, pack tuples et cetera. can skip through these if create couple of public cython cdef functions wrap methods on objects. generates cpython code handles these you.

a little example in silly object wrapped , embedded might (note, i'm using .c this, c++ has similar steps):

class pyclass(object):      def __init__(self):         self.data = []      def add(self, val):         self.data.append(val)      def __str__(self):         return "data: " + str(self.data)  cdef public object createpyclass():     return pyclass()  cdef public void adddata(object p, int val):     p.add(val)  cdef public char* printcls(object p):     return bytes(str(p), encoding = 'utf-8') 

compiling cython pycls.pyx (use --cplus c++) generate .c , .h file containing source , function declarations respectively. need create main.c file starts python , you're ready call these functions:

#include "python.h"   // python.h gets included first. #include "pycls.h"    // include header file.  int main(int argc, char *argv[]) {     py_initialize();   // initialize python     pyinit_pycls();    // initialize module (initpycls(); in py2)     pyobject *obj = createpyclass();     for(int i=0; i<10; i++){         adddata(obj, i);     }     printf("%s\n", printcls(obj));     py_finalize();     return 0; } 

compiling proper flags (which can obtain python3.5-config of python-config [py2]):

gcc pycls.c main.c -l$(python3.5-config --cflags) -i$(python3.5-config --ldflags) -std=c99 

will create executable interacts object:

./a.out data: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

all done using cython along public keyword generates .h header file. alternatively compile python module cython , create header/handle additional boilerplate yourself. since don't think want boggled down c-api learning, shouldn't way go.

as @freakish states in comment, ideal extract data (numpy has c-api can use this) , work on in pure c++. if work loops in c/c++ , perform grunt work there, you'll speed ups.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo