Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Python Compilers for Code Optimization: 8 Options

· 7 min read · Home Tech

In this article
  1. Why Python is slow and what compilers change
  2. The 8 options at a glance
  3. How to choose based on your workload
  4. Common problems and misconceptions
  5. Performance comparison and real-world numbers
  6. Safety, portability, and maintenance
  7. Alternatives to compilers
  8. Frequently Asked Questions

Python compilers and JIT (Just-In-Time) frameworks can speed up CPU-bound code by 2–100× depending on the workload, but no single tool fits all tasks. This article compares 8 concrete options—PyPy, Numba, Cython, mypyc, Codon, Shed Skin, Pyston, and Pyjion—and explains which one to pick for numeric loops, data pipelines, existing C extensions, or pure-Python distribution.

Why Python is slow and what compilers change

CPython, the reference interpreter, executes bytecode with a global interpreter lock (GIL) and dynamic type checks on every operation. A loop like for i in range(10**8): x += i runs at roughly 30–70 ns per iteration in CPython, while the same loop in C runs at ~1 ns. Compilers close that gap by either translating Python to machine code ahead of time (AOT) or by tracing and JIT-compiling hot loops at runtime.

The key distinction is type specialization. When a compiler knows x is an integer and lst is a list of floats, it can skip the runtime dispatch and emit direct CPU instructions. The 8 tools below differ in how they obtain that type information, what Python features they support, and whether they require you to change your code.

The 8 options at a glance

  • PyPy – a drop-in JIT replacement for CPython. Best for long-running, pure-Python programs with loops. Speeds up 2–10× on average, but breaks C extension compatibility.
  • Numba – a JIT for numeric Python. Decorating functions with @njit compiles them to machine code via LLVM. Ideal for NumPy array math and tight loops. Not for strings, dicts, or classes.
  • Cython – a superset of Python that compiles to C. You add static type declarations (cdef int i) to get C-like speed. The most mature option for wrapping existing C/C++ libraries.
  • mypyc – compiles Python modules with type annotations to C extensions. Uses standard typing syntax, so it works with mypy-checked code. Good for library authors who want speed without a separate language.
  • Codon – a Python-to-native compiler that uses type inference and LLVM. Claims 10–100× speedups on numeric code, but supports only a subset of Python (no dynamic features like monkey-patching).
  • Shed Skin – an experimental AOT compiler that translates a restricted Python subset to C++. Supports only Python 2.7 and requires type inference. Largely unmaintained, but historically useful for numeric scripts.
  • Pyston – a fork of CPython with a JIT. Originally standalone, now available as a pip package (pyston_lite) that speeds up existing code 10–30% with no changes. Best for drop-in acceleration.
  • Pyjion – a JIT for CPython that uses .NET Core. It is experimental and not recommended for production, but it shows how a pluggable JIT could work.

How to choose based on your workload

Start by profiling your code. If 90% of the time is inside a few numeric loops, Numba or Cython will give the best return. If the whole program is pure Python and you cannot change it, PyPy is the simplest win. If you are distributing a library, mypyc or Cython lets you ship a compiled extension without forcing users to install a JIT.

Concrete decision tree:

  • Numeric loops + NumPy arrays → Numba (add @njit).
  • Existing C library → Cython (write a .pyx wrapper).
  • Pure Python, no C extensions → PyPy (run pypy script.py).
  • Type-annotated library → mypyc (compile with mypyc mypackage/).
  • Standalone executable or extreme speed → Codon (if your code fits its subset).
  • Need to keep CPython and just get a small boost → Pyston lite.

Common problems and misconceptions

“PyPy will speed up everything.” No. PyPy’s JIT shines on long-running loops with stable types. Short scripts, I/O-bound code, and programs that spend time in C libraries (like regex or pandas) often see no benefit or even slowdowns.

“Numba works on any Python function.” It works on a subset. If you call len() on a list, use a dict comprehension, or raise an exception, Numba may fall back to object mode (slow) or fail to compile. You must stick to NumPy arrays, scalars, and simple loops.

“Cython requires learning a new language.” You can write pure Python in a .pyx file and get a modest speedup from compilation alone. Adding cdef types gives the big gains, but it is optional.

“Compiled code is always faster.” Not always. The overhead of copying data between Python objects and C arrays can dominate. For small datasets, the interpreter overhead is negligible, and the compile time of Numba or Cython may exceed the saved runtime.

Performance comparison and real-world numbers

On a typical CPU-bound loop (e.g., summing 10 million integers), rough speedups relative to CPython are:

  • PyPy: 5–10×
  • Numba (@njit): 50–100×
  • Cython (with cdef): 50–100×
  • mypyc (with annotations): 2–5×
  • Codon: 10–100× (but only for supported code)
  • Pyston lite: 1.1–1.3×

These are order-of-magnitude estimates, not benchmarks. The actual gain depends on the algorithm, memory access patterns, and whether the workload is vectorizable. For example, a recursive Fibonacci function will not speed up much because the bottleneck is function calls, not arithmetic. A matrix multiplication using NumPy is already fast because NumPy calls BLAS; Numba will not help unless you write the loops yourself.

Safety, portability, and maintenance

When you compile Python, you lose some of the language’s dynamic safety. Numba and Cython will raise compile-time errors for type mismatches that CPython would only catch at runtime. That is usually a good thing, but it means you must test thoroughly after adding decorators.

Portability is another concern. PyPy is a separate interpreter, so you need to install it on every target machine. Cython and mypyc produce platform-specific C extensions, so you must build them for each OS and Python version. Codon compiles to a native binary, which is easy to distribute but ties you to a specific CPU architecture.

Maintenance risk varies: PyPy and Cython are actively maintained; Numba is well-supported by Anaconda; mypyc is maintained by the mypy team; Codon is newer and less battle-tested; Shed Skin and Pyjion are effectively abandoned. For production code, choose a tool with an active community and a clear upgrade path.

Alternatives to compilers

Before reaching for a compiler, consider algorithmic fixes: use collections.Counter instead of manual dict counting, replace Python loops with NumPy vectorized operations, or use multiprocessing to parallelize across cores. These changes often give 10× speedups with no compilation. For I/O-bound workloads, asyncio or a thread pool may be more effective than any JIT.

If you need to call a fast library, consider using ctypes or cffi to wrap a C function directly. That avoids the overhead of Python-level loops entirely and is often simpler than Cython for small wrappers.

Frequently Asked Questions

Does PyPy work with NumPy and pandas?

PyPy has a NumPy port (numpy-py) that is mostly compatible, but pandas is not fully supported. If your workload depends on pandas, stick with CPython and use Numba or Cython for the slow parts.

Can I use Numba with pandas DataFrames?

Numba does not understand pandas objects directly. You must extract the underlying NumPy arrays (df['col'].values) and pass those to a @njit function. That works well for row-wise operations, but you lose pandas’ convenient indexing.

Is Cython still worth learning in 2025?

Yes, especially if you need to interface with C/C++ libraries or distribute compiled extensions. Cython is stable, well-documented, and used by many scientific packages. For pure-Python speedups, mypyc is easier, but Cython gives finer control over memory and types.

What is the fastest way to speed up a Python script without changing code?

Run it with PyPy if it is pure Python, or install pyston_lite in your existing CPython environment. Both require no code changes. For a quick win on numeric code, add @njit to the hottest function—that is a one-line change.

Does compiling Python eliminate the GIL?

No. PyPy has a GIL, and Cython extensions still hold the GIL unless you explicitly release it with with nogil:. Numba releases the GIL inside @njit functions by default, so you can use threads for parallel speedup, but the rest of the Python program remains single-threaded.

Can I ship a compiled Python program as a standalone executable?

Yes. Cython can compile to a C extension (not standalone), but tools like PyInstaller can bundle the interpreter and your compiled modules into a single executable. Codon compiles to a true native binary with no Python runtime, which is the closest to a standalone executable.