- C: A compiler translates the entire program into machine code before it runs. The generated instructions directly perform integer arithmetic and loop control; the CPU executes them with very low per-iteration overhead.
- CPython: When you run Python, source code is compiled to bytecode. The CPU does not execute Python bytecode directly. Instead, the CPython interpreter fetches, decodes, and dispatches each bytecode instruction at runtime. That fetch–decode–dispatch loop adds overhead on every iteration.
total = total + i looks like:
add instruction:
- Fetches the bytecode instruction to load operands (e.g.,
LOAD_FASTfor locals orLOAD_NAMEfor globals). - Resolves names to
PyObject*pointers and pushes them onto the interpreter’s evaluation stack. - Fetches the
BINARY_OPbytecode and performs runtime checks to determine the operation semantics (e.g., integer addition vs. string concatenation). - Calls the appropriate implementation routine that handles the operation on Python objects.
- Stores the resulting
PyObject*viaSTORE_FASTor another store opcode.
- Dynamic typing: The interpreter must inspect operand types at runtime to pick the right operation implementation. This flexibility (a variable can hold ints, strings, lists, etc.) is powerful, but it requires additional dispatch and checks.
- Python integers are objects: In CPython, integers are full
PyObjectinstances (including type information and reference counts) and are immutable. An expression liketotal + itypically creates a new integer object and adjusts references, instead of updating a primitive in place. - Allocation overhead: Creating and destroying millions of small objects incurs CPU and memory costs. On many 64-bit CPython builds, a small
PyLongcan be dozens of bytes (commonly cited near 28 bytes), so frequent allocations add up.
- Specialized bytecode opcodes for common patterns (e.g.,
LOAD_FAST,LOAD_CONST) to speed up common cases. - Adaptive and specializing interpreter efforts introduced around Python 3.11 to reduce dispatch costs for repeated patterns.
- Internals like object reuse and small-integer caches mitigate some allocations, but they cannot fully eliminate the cost of Python’s dynamic semantics.

Python’s runtime overhead is often irrelevant for many real-world programs. Most applications spend a large fraction of their time waiting on I/O (network, database, or disk). In those cases, Python’s developer productivity, readability, and ecosystem often outweigh raw loop performance.
Recommended choices by use-case:
- Numeric loops and array math: use
NumPy,Cython, orNumba. - A long-running server where Python overhead matters: consider
PyPy(if extensions compatible) or refactor hotspots to C extensions. - Quick fix or one-off hotspot: write a small C extension or use
Cython. - I/O-bound applications: optimize concurrency (async I/O, multiprocessing) before rewriting in C.
- CPython docs — bytecode and opcode reference
- Python 3.11 release notes — specialization and performance
- Cython — compile Python-like code to C
- Numba — JIT compiler for numeric Python
- PyPy — JIT-enabled alternative Python runtime
- NumPy — array computing for Python