Will I see improvement in speed if I compile my python files to cython? Or do I need to rewrite my code in cython to actually see the improvement?
I'm doing like this below.
python convert_to_cython.py build_ext --inplace
Will I see improvement in speed if I compile my python files to cython? Or do I need to rewrite my code in cython to actually see the improvement?
I'm doing like this below.
python convert_to_cython.py build_ext --inplace
As it stands, this question is much too broad because the answer depends greatly on what your Python code looks like. Without seeing it, or at least having some idea of what it does, it's not possible to give you a straightforward answer.
If your code mainly calls functions from other compiled libraries then you may well see no improvement, or even slower runtimes. For example, there are lots of questions on SO from people who can't understand why Cython doesn't magically speed up their numpy code. However, things like nested Python for
loops can be very good candidates for speeding up using Cython.
Your first step should always be to profile your code (e.g. using the excellent line_profiler
). Once you've identified where the bottlenecks are then you can think about how to speed them up, possibly by rewriting them in Cython. It will almost certainly be a waste of time for you to go and rewrite all of your existing Python code in Cython.
Although this question is quite broad but in general terms YES. It does speed up your code and sometime in the order of 100.
For reference, the Cython docs says and I quote
However, for performance critical code, it is often helpful to add static type declarations, as they will allow Cython to step out of the dynamic nature of the Python code and generate simpler and faster C code - sometimes faster by orders of magnitude
The main reason why languages like C/C++ are faster is that they create machine-dependent assembly language to tweak all the optimization which are hardware dependent. This is achieved using compilers mostly because few things like
etc..
Now One of the important feature Statically typed variable is extensively used by Cython. As python variables are typeless and C variables are not, Cython can give users flexibility to statically fix the type of their variables.
In Cython doc website they showed how just by mentioning the type actually produced 35% faster performance.
Note However my final word is that you be careful while converting your Python code in Cython because you may be using some frameworks/APIs in your project which doesn't have support for Cython. Sometime even if you convert your code into Cython, that hardly changes anything inside. So it all depends on your code.
Hence first make sure of complete portability of your Python code in Cython and also check if that is absolutely necessary.
Edit 1 One more thing is that converting you code in Cython, makes it less readable so please be mindful of that.
Again Quoting from Cython Docs
It must be noted, however, that type declarations can make the source code more verbose and thus less readable. It is therefore discouraged to use them without good reason, such as where benchmarks prove that they really make the code substantially faster in a performance critical section
Edit 2 Answering for the question Or do I need to rewrite my code in cython to actually see the improvement?
No because the Cython compiler does the whole thing for you.
The Cython compiler will convert it into C code which makes equivalent calls to the Python/C API.
As Cython can accept almost any valid python source file, one of the hardest things in getting started is just figuring out how to compile your extension.
For more info visit this