From 2x to 100x: Real-World FFI Speed Leaps

Depending on the workload, native code extensions can deliver substantial performance gains. Examples include Cython achieving over 100x speedup for numerical bottlenecks, and ctypes providing up to a 2x speedup compared to Pybind11 in certain workloads. The most significant boosts are realized by minimizing Python interaction within critical loops.

The top 3

  1. Levenshtein Distance: 150x Python FFI Speed Leap: Optimizing Levenshtein distance calculations with a C extension (a form of FFI) can achieve over 150 times faster execution compared to a pure Python implementation.
  2. Python C Extension: 100x for Prime Finding: Rewriting computationally intensive tasks like a prime-finding function into a C module for Python can lead to a remarkable 100x increase in execution speed.
  3. FFI Loop Inversion: 100x Faster C Function Calls: When calling a C function repeatedly from Python, inverting the loop to execute within C++ via FFI (e.g., using cppyy) can yield a 100x speedup compared to a Python-driven loop.

Sources

Open the full topic