13

Basically I want to make a Python program call functions written in C.

So (as far as I know) my options are:

  • CTypes/CFFI
    • Create a DLL/SO/DyLib containing the C functions and access them using CTypes or CFFI. Apparently CFFI is way faster with the only drawback of having to declare in python all the functions signatures.
    • Pros:
      • Don't have to make any adaptation in my C functions. All type-translation is done in Python.
    • Cons:
      • Performance ?
  • Python Binary Module
    • Write a python interface in C, converting my C module into a binary python module
    • Pros:
      • Performance ?
    • Cons:
      • All type-translation is done in C. Using [SIP][3] this might be automated.

Convert the C module into a python binary module is really faster ?

Does both solutions support sending python callbacks to C functions ?

Is SIP a good option to generate a python interface ? Are there any other options ?

Are there any other particularities in any of them ?

romulof
  • 357
  • 2
  • 12
  • 1
    Found some good stuff: [5 ways to use Python with native code](http://kos.gd/2013/01/5-ways-to-use-python-with-native-code/), [Python Wrapper Tools: A Performance Study](http://web.archive.org/web/20070703071726/http://people.web.psi.ch/geus/talks/europython2004_geus.pdf) – romulof Jul 19 '13 at 21:40

1 Answers1

1

I was just reviewing an old list of options I published related to this: http://stromberg.dnsalias.org/~strombrg/speeding-python/

If you're only targeting CPython (2.x or 3.x), I'd probably go for Cython.

If you want to be able to run on Pypy too, CFFI might be good; I've not tried it yet, but it sounds great. It's not entirely like ctypes though - ctypes is more ABI level, while CFFI is more API level - which is nice.

If you want to be able to run on Jython too, subprocess is probably your best bet.

dstromberg
  • 6,954
  • 1
  • 26
  • 27
  • 2
    The C functions are already written. Tons of stable code. I'm only looking for a way to use then in python. – romulof Jul 19 '13 at 21:39
  • That's interesting, though my suggestions probably still apply. – dstromberg Jul 19 '13 at 22:03
  • Supposedly ctypes is slow at the python<->C boundary. They've optimized this for some common uses in Pypy. – dstromberg Jul 19 '13 at 22:25
  • 1
    I'm being told at pyconuk this week that using cffi is nowadays faster than using ctypes, especially for applications like OpenGL where you're constructing arrays of C types and then frequently modifying slices of them and re-sending to the C drivers. – Jonathan Hartley Sep 22 '13 at 09:10
  • You also need to consider your dev platform. Linux is not a problem but windows is restricted to one particular compiler fot CFFI. You will have to check for compiler compatibility with any other packages you are using if you opt for CFFI on Windows – cup Dec 29 '22 at 06:16