Cython VS C ++ Performance Comparison?

I am trying to use Cython for my project code.

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

As I move on, I'm a little confused. As far as I understand, Cython wraps Python code in C. Performance is improved since C has better computational performance. I will fix it?

If I'm right, do I need to write .dll in C ++ and call it from Python to improve performance?

If I write python code and port it to C, then it is called from Python, does it work better than a .dll call written in C ++?

+6
source share
1 answer

First of all, let me dissolve 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 suggests that "driving a car at a given speed is slower than when using an F1 racer on a car with the same speed." It just doesn't make sense. When Python loads your library, it loads and processes it in the same way that the kernel loads and processes (in fact, the kernel does this in the case of Python). In fact, this “dual load” (which was not the original design for dynamic libraries) can slow down your library. I must emphasize that this is a tiny difference and should not concern the ordinary programmer.

  • Cython Wraps Python C Code

This is not true. It compiles python code in C, which is then compiled into a dynamic library to load Python later. This can slightly improve your Python code and give you the ability to interact with atomic C data types using a small Python sauce. Although this is pretty cool, it does not give your code any “magic” abilities.

I would also like to add that some tests proved that Java (drum) is actually faster than C, C ++, Python and other languages, because the JVM is very optimized. This does not mean that you should use Java (because it has other problems), but it should give perspective.

-1
source

Source: https://habr.com/ru/post/955251/


All Articles