The `cdef` and `cpdef` Speed Dial

Cython's `cdef` keyword defines C-speed variables and functions that are primarily for internal Cython/C calls, offering maximum performance, while `cpdef` creates hybrid functions providing both C-speed internal access and callable Python interfaces.

The top 3

  1. `cdef` vs. `cpdef`: The Performance Divide: `cdef` functions are purely C-level, offering maximum speed and callable only from Cython, while `cpdef` functions generate both fast C and Python-callable versions, balancing speed with Python interoperability.
  2. `cdef`'s Raw Speed: Bypassing Python Overhead: Declaring variables and functions with `cdef` and static types in Cython can achieve significant speedups, often 10x to 100x, by enabling direct C-level operations and eliminating Python's dynamic typing overhead.
  3. `cpdef`: Bridging Python and C Efficiency: `cpdef` functions are uniquely designed to automatically create both a fast C function for internal Cython calls and a Python-callable wrapper, enabling seamless and efficient interoperability between Python and C codebases.

Sources

Open the full topic