NumPy's Top 3 Bottlenecks: 100x Speedup Targets

The most significant speed gains with Numba or Cython for NumPy code are typically found in explicit Python loops, element-wise mathematical operations (like `sqrt` or `exp`), and individual array element indexing, which can be 100x to over 1000x faster when compiled.

The top 3

  1. Python For-Loops: NumPy's #1 Performance Killer: Direct iteration over NumPy arrays using Python's native `for` loops is consistently the single largest performance bottleneck, often 10-100 times slower than vectorized operations or compiled code due to the high overhead of the Python interpreter per iteration.
  2. Custom Python Functions: `np.vectorize`'s Hidden Trap: Applying complex, non-vectorized custom Python functions element-wise to large NumPy arrays, even with helper functions like `np.vectorize` or `np.apply_along_axis`, often creates a severe bottleneck by repeatedly invoking the slow Python interpreter for each element.
  3. Indexing Bottlenecks: Costly Copies from Fancy Indexing: Unlike basic slicing, NumPy's 'fancy indexing' (using integer arrays or boolean masks) almost always returns a copy of the data, not a view. This can lead to significant performance and memory overhead for large arrays, as new memory is allocated and data copied.

Sources

Open the full topic