0

I was learning Python and came across something called Cython helping to gain performance improvements. Later, I found this talk https://www.youtube.com/watch?v=_1MSX7V28Po presented by the developer of Instagram and during his talk he said that Cython is truly helpful in terms of Python optimizations. But briefly mentioned that if you just add Cython is good but if you add types annotations for your python code. You can achieve much better performance. So, the question is Is it true that by just adding cython we can get performance boost BUT if we add TYPE ANNOTATIONS for python code, then performance boost will be much more better?

6
  • 1
    The typed variables in Cython are distinct from Python type annotations Commented May 17, 2020 at 5:56
  • Using Cython typed variables in Cython will provide a performance benefit. typing-style type annotations provide no performance benefit; at worst, they may actually slow your code down. Commented May 17, 2020 at 5:58
  • @juanpa.arrivillaga, I mean if we use type system of cython, will it give us additional and better performance boost compared to just adding cython and not using type system of it in our python code? Commented May 17, 2020 at 6:03
  • 4
    Yes, that's the whole point of Cython Commented May 17, 2020 at 6:16
  • 3
    Spend time reading the cython docs. Tutorials and blogs only give you suggestions, not the real deal. And at the start, don't throw cython at all of your code. Focus on some functions that really need the speed improvement. You can loose a lot of Python's flexibility if you switch to a compiled version too soon. Commented May 17, 2020 at 7:40

1 Answer 1

0

Taking a look at the documentation I think we can answer your question.

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.

Example without type annotations:

def f(x): return x ** 2 - x def integrate_f(a, b, N): s = 0 dx = (b - a) / N for i in range(N): s += f(a + i * dx) return s * dx 

Simply compiling this in Cython merely gives a 35% speedup. This is better than nothing, but adding some static types can make a much larger difference.

Example with type annotations:

def f(double x): return x ** 2 - x def integrate_f(double a, double b, int N): cdef int i cdef double s, dx s = 0 dx = (b - a) / N for i in range(N): s += f(a + i * dx) return s * dx 

Since the iterator variable i is typed with C semantics, the for-loop will be compiled to pure C code. Typing a, s and dx is important as they are involved in arithmetic within the for-loop; typing b and N makes less of a difference, but in this case it is not much extra work to be consistent and type the entire function.

This results in a 4 times speedup over the pure Python version.

So it appears that just using Cython does provide and performance boost, and that adding type annotations provides even more of a performance boost.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.