Python's Object Model: Why C Types Are Faster

The Python object model's dynamic typing and attribute lookup add significant runtime overhead; C API allows direct manipulation of fixed-layout C structures, bypassing costly runtime checks and indirections.

The top 3

  1. Python Ints vs. C: Up to 7x Memory Cost: A small Python integer (e.g., 42) consumes at least 28 bytes, while a C `int` typically uses just 4 bytes, showcasing Python's significant memory overhead due to its flexible, arbitrary-precision design and object structure.
  2. Python's Dynamic Tax: Slower Basic Operations: C types integrated into Python bypass runtime type checks, method lookups, and other dynamic overheads, making fundamental operations like arithmetic and data access significantly faster than with native Python objects.
  3. C's Contiguous Data: Cache Efficiency King: C arrays store elements contiguously in memory, enabling CPU cache prefetching and minimizing cache misses, which dramatically speeds up iterative data access compared to Python lists that store scattered pointers to individual, larger objects.

Sources

Open the full topic