2

Performance is really a key here. I have some C functions for numeric tasks, of double input and double output. I am looking for a highly efficient way to invoke these functions and retrieve the results. What would be the fastest solution?

Many solutions exist, such as ctypes, swig, etc. Some of these tools, e.g., swig, have evolved significantly over the years and thus updated information can be helpful. To date, performance-wise, what is the best solution for calling existed C code from Python script?

[Edit] This question is related to, but different from this question. As mentioned above, I am seeking updated info whereas that post is dated on 2008. In addition, that post is about the quickest way to do to save the developer's time, and is not for application performance as I am asking here.

Community
  • 1
  • 1
zell
  • 9,830
  • 10
  • 62
  • 115
  • Possible duplicate of [Calling C/C++ from python?](http://stackoverflow.com/questions/145270/calling-c-c-from-python) – Sazzad Hissain Khan Dec 25 '15 at 15:53
  • Thanks. That post is on 2008 I believe. In addition, that post is about the quickest way to do to save the developer's time, and is not for application performance. – zell Dec 25 '15 at 16:09
  • Calling C lib functions via ctypes is pretty fast. Building Python floats from C doubles is a bit slow, so keep intermediate results in C form. – PM 2Ring Dec 25 '15 at 16:28
  • 1
    @zell: the fastest way is going to be some raw primitive, which already existed in 2008. – Karoly Horvath Dec 25 '15 at 16:31
  • @PM 2Ring. Thanks. You comments are helpful. How can I save time from transforming between C double and Python doubles? – zell Dec 25 '15 at 16:32
  • @zell I think you should use either use http://stackoverflow.com/a/145649/1422096 (which I did, succesfully) or use Cython. – Basj Dec 25 '15 at 16:34

1 Answers1

2

I would consider writing a C extension module.

This way, you can keep the most critical parts in C, you can have the compiler optimize your code as much as possible and you can avoid converting certain values to Python objects.

Cython is a nice way for writing C extensions without using C.

Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69