0

What is the fastest way to call C/C++ functions and use C++ classes? There are various methods to do this such as Python Extension Module (Python.h), Cython, SWIG, Boost, and etc.

I've already implement C/C++ functions and C++ classes. Because performance is very important in my project.

So, I want to call C/C++ functions and use C++ classes in python with minimal modification of c/c++ code (or wrapping with no change of existing code). What is the best way?

Dayamre
  • 1,907
  • 2
  • 13
  • 10
  • 2
    No modification on the C++ side -> Cython; minor modification on the C++ side -> SWIG; C++ only solution -> Boost. – Henri Menke Apr 28 '17 at 02:22
  • are you doing a lot of calculations or what? why not using C/C++ without Python? – Azat Ibrakov Apr 28 '17 at 02:32
  • 1
    If they are compiled into a shared object, [`ctypes`](https://docs.python.org/library/ctypes.html) can be used. – metatoaster Apr 28 '17 at 02:35
  • 1
    Asking about fastest is a question that can rarely be meaningfully answered. Fastest to do what? I think only experimentation with your specific use case can tell you that for sure. Also the methods with less runtime overhead will doubtless have larger development time overhead. – Paul Rooney Apr 28 '17 at 02:47

1 Answers1

2

If you already have the functions in C++, I suggest you create a dll or more and check how they can be called from python. As far as I know there is a library in python for things like this named ctypes and it works like this:

int sum(int a, int b)
{
    return a + b;
}


import ctypes
a = ctypes.WinDLL ("path\\to\\mydll.dll")

result = a.sum(10,1)

print(result)