Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cython VS C++ Performance Comparison? [closed]

I am trying to use Cython to code my project.

My plan is to write .dll in C++, and call them from Python via Cython. So I can have high computational performance of C++, while keeping the simplicity of development of Python.

As I go further, I am a bit confused. As I understand, Cython wraps python code into C. The performance is improved since C has better calculation performance. Am I correct at this?

If I am right above, then is it necessary to write .dll in C++ and call it from Python in order to improve the performance?

If I write python code and wrap it into C, then call it from Python, does it perform better than calling .dll that written in C++?

like image 971
ChangeMyName Avatar asked Oct 04 '13 13:10

ChangeMyName


People also ask

How fast is Cython compared to C?

The CPython + Cython implementation is the fastest; it is 44 times faster than the CPython implementation. This is an impressive speed improvement, especially considering that the Cython code is very close to the original Python code in its design.

How much slower is Cython than C?

Cython is the same speed as a carefully tuned C/C++ program; carefully tuned, Cython maps directly to C/C++. I've done many benchmarks of low level numerical code when implementing SageMath (which uses Cython for several 100K lines of code).

Why is Cython faster?

Despite being a superset of Python, Cython is much faster than Python. It improves Python code execution speed significantly by compiling Python code into C code. The compilation further helps developers to run the Python programs smoothly without deploying additional computing resources.

Is Cython slow?

Calling the Cython function is faster than calling a Python function call, it's true. But even 30 nanoseconds is rather slow by the standards of compiled languages: for comparison, a C function called by another C function might take only 3 nanoseconds, or much less if it gets inlined.


1 Answers

First of all, let me disband a few misconceptions that you seem to have.

  • Calling a library from another program will speed up your library.

No, no, no, no, no. This makes about as much sense as saying "driving a car at a set speed is slower than having a F1 racer drive a car at the same speed". It just makes no sense. When Python loads your library, it loads and processes it similar to how the kernel loads and processes it (in fact, the kernel does that in Python's case too). In fact, this "double loading" (which wasn't the original design for dynamic libraries) can slow down your library. I should emphasise that this is a tiny difference, and should not concern the ordinary programmer.

  • Cython "wraps" Python code into C

It doesn't. It compiles the python code into C, which is then compiled into a dynamic library for Python to load later. This may optimise your Python code somewhat, and give you the ability to interface with atomic C data types, with Python's magic sauce on top. While this is pretty cool, it doesn't give your code any "magical" abilities.

I would also like to add that some tests have proven that Java is (drum roll) actually faster than C, C++, Python and other languages because the JVM is very optimised. That doesn't mean you should use Java (because it has other problems), but it should give perspective.

like image 61
cyphar Avatar answered Sep 21 '22 11:09

cyphar