I have a simple plan for my program: do all input parsing and data preprocessing with Python and then pass a lot of structures and call ONCE number-crunching function that I'm going to write in C. Here goes my question: what would be faster (in terms of execution speed)? To call C function with ctypes or write it using cython?
Given an infinite amount of time to optimize your code Ctypes will probably be faster as you can push as much of the heavy lifting into compiled code as possible.
cython takes python-like code and converts it to C code with lots of caveats and exceptions. If cython were perfect, it would be as fast as coding in the C-API for python. cython is a fantastic tool for quick code dev, but is not quite that perfect yet.
Ctypes will require some manipulation of your input variables to get them into C cleanly. This may not be a problem if you are doing a fair bit of data preprocessing anyway.
As usual you need to decide how important the execution speed is compared with coding time/code readability/code reusability it would seem strange to me to be only interested in execution speed, and still want to code any of it in python.
Conversely, the general paradigms of python coding would tend to contrast with having large sections of C code held together by python wrappers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With