Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between Cython, extending C/C++ with Python.h, etc

Right now I have an image processing algorithm that is roughly 100 lines or so in Python. It takes about 500ms using numpy, PIL and scipy. I am looking to get it faster, and as the actual algorithm seems pretty optimized thus far, I'm wondering if using a different approach such as Cython would improve the times. I believe that I have several different things I could do:

  1. Use Cython to expose relevant parts of the C library to Python.
  2. Use Ctypes to just write everything in C but still have it pure Python (not leaning towards this at all)
  3. Create an extension module in C/C++ and them import it and call the functions. I'm not sure if I would be able to use numpy this way though.
  4. Create a DLL and have Python load it. This doesn't get to use numpy or those modules, but would still be very efficient.

I'm just looking for speed here, not worried about difficulty of the implementation. Is there any one option that is better in this case, are they all the same, or is it even worth doing?

like image 350
Chrispresso Avatar asked Jun 08 '16 20:06

Chrispresso


People also ask

Does Cython use C or C++?

Cython is a programming language that blends Python with the static type system of C and C++. cython is a compiler that translates Cython source code into efficient C or C++ source code. This source can then be compiled into a Python extension module or a standalone executable.

What is C extension in Python?

Any code that you write using any compiled language like C, C++, or Java can be integrated or imported into another Python script. This code is considered as an "extension." A Python extension module is nothing more than a normal C library. On Unix machines, these libraries usually end in . so (for shared object).

What C compiler does Cython use?

Windows The CPython project recommends building extension modules (including Cython modules) with the same compiler that Python was built with. This is usually a specific version of Microsoft Visual C/C++ (MSVC) - see https://wiki.python.org/moin/WindowsCompilers.

Is Cython compiled?

Cython is a compiled language that is typically used to generate CPython extension modules.


1 Answers

It helps to know what you need to do here.

If you're not using ctypes for function calls, it's unlikely that it will save you anything to just have ctypes types involved. If you already have some DLL lying around with a "solve it for me" function, then sure, ctypes it is.

Cython creates extension modules, so anything you can do with Cython could also be done with an extension module, it just depends on how comfortable you are writing extensions by hand. Cython is more limited than writing extension by hand, and harder to "see" performance in (the rules for optimizing Cython are basically the opposite of optimizing CPython code, and if you forget to cdef the right things, you gain nothing), but Cython is generally simpler too.

Writing a separate non-extension DLL is only worthwhile if you have non-Python uses for it; otherwise, a Python extension is basically just the DLL case, but better integrated.

Basically, by definition, with infinite time and skill, a CPython extension will beat any other option on performance since it can do everything the others do, and more. It's just more work, and easy to make mistakes (because you're writing C, which is error prone).

like image 52
ShadowRanger Avatar answered Oct 21 '22 10:10

ShadowRanger