Advanced Topics
Cython
Python superset that compiles to C for near-native speed
Interview: Speed optimization and scientific computing
Cython is a superset of Python that compiles to C extensions. You write mostly-Python code with optional C type declarations, and Cython generates optimized C code. It is the backbone of libraries like SciPy, scikit-learn, and pandas for their performance-critical paths.
How Cython Works
Cython translates .pyx files into C code, which is then compiled into a Python extension module. The key insight: adding static type declarations to Python variables allows Cython to generate direct C operations instead of Python object protocol calls.
Performance Gains
Typical speedups range from 10x to 100x for numerical code. A pure Python loop doing math might take 1 second; with Cython type annotations, the same code can run in 10ms.
When to Use Cython
- Numerical loops that cannot be vectorized with NumPy
- Wrapping existing C/C++ code for Python use
- Adding type annotations to hot paths in production libraries
Use Cases
Accelerating numerical Python code to C speed (10x-100x)
Building Python bindings for existing C/C++ codebases
Performance-critical paths in data science libraries
Replacing hot loops in production pipelines without rewriting in C
Common Mistakes
Not using cdef for variables in hot loops — negates most performance benefit
Mixing Python objects in tight loops — use typed memoryviews instead
Forgetting to add boundscheck=False / wraparound=False for array access
Using Cython when NumPy vectorization would suffice