What does it mean to vectorize for-loops in Python? Is there another way to write nested for-loops?
I am new to Python and on my research, I always come across the NumPy library.
What does it mean to vectorize for-loops in Python? Is there another way to write nested for-loops?
I am new to Python and on my research, I always come across the NumPy library.
Python
for
loops are inherently slower than their C counterpart.This is why
numpy
offers vectorized actions onnumpy
arrays. It pushes thefor
loop you would usually do in Python down to the C level, which is much faster.numpy
offers vectorized ("C levelfor
loop") alternatives to things that otherwise would need to be done in an element-wise manner ("Python levelfor
loop).The
numpy
vectorized addition was x70 times faster.Here's a definition from Wes McKinney:
Vectorized version:
The same thing with loops on a native Python (nested) list:
How do these two operations compare? The NumPy version takes 436 ns; the Python version takes 3.52 µs (3520 ns). This large difference in "small" times is called microperformance, and it becomes important when you're working with larger data or repeating operations thousands or millions of times.