guide 05 / 08
CPython vs Cython: Interpreter vs Compiler, Explained
CPython vs Cython: one runs Python, the other compiles Python-like code to C extensions. Compare purpose, speed, workflow and when to use Cython with CPython.
TL;DR
- CPython is the Python interpreter. Cython is a compiler that produces C extensions.
- Cython output runs inside CPython, so they work together rather than compete.
- Cython helps most in numeric loops once you add C type declarations.
- Most projects never need Cython. Profile before you optimize.
01Different jobs entirely
Despite the near-identical names, these aren't competitors:
- CPython runs Python programs. It is the standard interpreter.
- Cython translates
.pyxfiles, which are Python plus optional C types, into C. That C is compiled into an extension module that CPython imports.
02Comparison table
| CPython | Cython | |
|---|---|---|
| Type | Interpreter / runtime | Source-to-source compiler |
| Input | .py files | .pyx / .py files |
| Output | Program execution | C code → compiled extension module |
| Needs a C compiler? | Only to build CPython itself | Yes, for every build |
| License | PSF License | Apache 2.0 |
| Install | python.org or source | pip install cython |
03How they work together
- You write
fast.pyx. - Cython generates
fast.c. - A C compiler builds
fast.cpython-315-x86_64-linux-gnu.so(the name includes the CPython version). - CPython imports it with
import fast.
The extension is tied to CPython's C API, which is why the filename includes the interpreter version and platform.
04When Cython is worth it
- Tight numeric loops that you can't vectorize with NumPy.
- Wrapping existing C libraries with a Pythonic interface.
- Releasing the GIL around pure-C sections for parallel work.
Plain Python code compiled by Cython without type declarations often gains only a little. The big wins come from cdef types.
05When to skip it
- I/O-bound code such as web requests or databases, where the CPU isn't the bottleneck.
- When a library already provides a fast path, such as NumPy, pandas or the standard library's C accelerators.
- When your team can't maintain a compiled build step.
Always profile first. CPython 3.15 even ships a high-frequency statistical sampling profiler (PEP 799) to help you find real hotspots.
06Packaging Cython code
Cython modules must be compiled for each platform and CPython version. Modern packaging tools such as setuptools, meson-python and scikit-build-core can build them, and CI services build wheels so users don't need a compiler. Remember that each new CPython feature release, like 3.15, needs fresh wheels. That's another reason library authors test early against the CPython main branch.
07A tiny benchmark mindset
To see whether Cython helps, follow a simple routine:
- Write the function in plain Python and time it with
timeit. - Compile the same file unchanged with Cython and time it again. Expect a modest gain at most.
- Add
cdeftypes to the loop variables and accumulators, then time it once more. - Check whether NumPy vectorization would solve the problem without a build step.
Only keep the Cython version if the measured gain justifies the extra build complexity for your team and your users.
08Real-world examples
Many well-known projects use Cython for performance-critical parts. Scientific libraries use it to speed up inner loops, and web tooling has used it to accelerate parsing. In every case the pattern is the same: most of the project stays in readable Python on CPython, and a small number of hot functions are compiled with Cython. That balance gives you speed where it counts without turning the whole codebase into C.
09Try both
Install Cython with pip, and grab CPython's source to see how its own C accelerators are written:
??Quick questions
Q01Is Cython faster than CPython?
Cython isn't a runtime, so it can't be faster than CPython. It produces extension modules that run faster than equivalent pure Python inside CPython.
Q02Can I use Cython without CPython?
In practice, no. Its output targets CPython's C API.