Can Numba or Cython speed up Python? Yes—especially CPU-bound numerical loops—but the gain depends on the workload, data types, compilation cost and baseline. Neither library guarantees a million-fold speedup. This guide explains when to choose each, how to run a reproducible comparison, and why an earlier microbenchmark on this page was misleading.
Numba vs Cython: what each tool does
Numba uses just-in-time compilation to optimize supported Python and NumPy numerical operations, commonly with @njit. Its first call may include substantial compilation time; later calls can reuse the compiled specialization. Numba does not automatically accelerate arbitrary Python objects or pandas DataFrame operations.
Cython translates Python-like code into C/C++ extension-module source and can use explicit C types to reduce interpreter overhead. Building an extension requires a compiler and packaging step. C integer types have fixed ranges, so a naive accumulator may overflow even when Python integers would not.
Before compiling, choose a better algorithm
The sum 0 + 1 + ... + (n - 1) has a closed-form solution, n * (n - 1) // 2. It runs in constant time for ordinary bounded-size integer inputs and is more appropriate than compiling a billion-iteration loop. A compiler cannot substitute for choosing the right algorithm. For large Python integers, arithmetic costs still depend on integer size.
def series_sum_formula(n):
if n < 0:
raise ValueError("n must be nonnegative")
return n * (n - 1) // 2
assert series_sum_formula(10) == 45
A reproducible Numba benchmark
Install the dependencies with python -m pip install numba numpy in a supported Python environment. Run the following as a script. It measures the first JIT call separately and compares equivalent outputs after warm-up. The exact timings depend on your machine and versions; no benchmark result is claimed here.
from time import perf_counter
from statistics import median
from numba import njit
def series_sum_python(n):
total = 0
for i in range(n):
total += i
return total
@njit
def series_sum_numba(n):
total = 0
for i in range(n):
total += i
return total
def benchmark(func, n, repeats=7):
samples = []
for _ in range(repeats):
start = perf_counter()
result = func(n)
samples.append(perf_counter() - start)
return result, median(samples)
n = 1_000_000
start = perf_counter()
compiled_result = series_sum_numba(n) # includes first-call JIT compilation
compile_and_first_call = perf_counter() - start
py_result, py_seconds = benchmark(series_sum_python, n)
nb_result, nb_seconds = benchmark(series_sum_numba, n)
expected = n * (n - 1) // 2
assert py_result == nb_result == compiled_result == expected
print("First Numba call (s):", compile_and_first_call)
print("Python median (s):", py_seconds)
print("Warmed Numba median (s):", nb_seconds)
print("Speedup:", py_seconds / nb_seconds if nb_seconds else "below timer resolution")
Important: The compiler may recognize and simplify this particular arithmetic loop, making it a poor representative of real-world numerical workloads. Very short timings approach timer resolution; use longer or repeated workloads, and compare against a NumPy vectorization or a mathematical formula when appropriate. Numba’s integer arithmetic may have fixed-width overflow behavior unlike Python’s arbitrary-precision integers.
A Cython implementation (requires a build)
Save the following in sum_series.pyx. It uses C integer types for speed, but is intentionally limited to inputs small enough to avoid signed 64-bit overflow. A full Cython benchmark requires compiling this module with a compatible C compiler; the code below is illustrative and has not been compiled in this review.
# sum_series.pyx — Cython source, not plain Python
cpdef long long series_sum_cython(long long n):
cdef long long total = 0
cdef long long i
for i in range(n):
total += i
return total
For a simple build in an environment with Cython and a C compiler, create setup.py with from setuptools import setup and from Cython.Build import cythonize, then call setup(ext_modules=cythonize('sum_series.pyx')). Run python setup.py build_ext --inplace from the same folder. Then import series_sum_cython from sum_series and compare its output with the Python function for a safe input size. Refer to the official Cython build guide for maintained build instructions.
Historical benchmark charts (2020; not independently reproducible)
The following three original images are preserved for historical context. Their hardware, environment, source revisions, measurement methodology and raw timing data are not sufficiently documented to validate their numerical speedup claims. In particular, comparing a nearly constant-time optimized result with an O(n) Python loop can produce spectacular but misleading ratios. Do not treat the charts as current verified performance benchmarks.



How to benchmark fairly
Use the same input and verify identical results. Warm up Numba before measuring steady-state performance, but also report first-call latency for short-lived applications. Use a suitable number of repetitions, a robust timer, and report Python/Numba/Cython/NumPy versions, CPU, operating system, compiler flags and sample sizes. Include an optimized algorithmic baseline rather than comparing only against an intentionally slow loop.
Troubleshooting and frequently asked questions
Why did @njit fail? The function may use unsupported Python features or object types. Simplify the numerical kernel and consult Numba’s supported Python features. Why is the first call slower? Compilation happens at first use for a new signature. Does Numba speed up pandas? Not automatically; isolate numeric arrays or use documented pandas integrations when supported.
Which should I choose? Start with profiling and algorithmic improvements. Try NumPy vectorization for array operations, Numba for compatible numerical loops, and Cython when you need compiled extensions or more explicit control. For related Python techniques, see PickupBrain’s list comprehension guide.

C P Gupta is a YouTuber and Blogger. He is expert in Microsoft Word, Excel and PowerPoint. His YouTube channel @pickupbrain is very popular and has crossed 9.9 Million Views.

This is very useful info. Looks like numba rocks and it’s already in my current installation of python 3. Thank you!
Thanks dear for the comments. Yes, Numba rocks and I have been using this regularly.
Will numba increase brute force speed?
Welcome dear.