Skip main navigation

Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. T&Cs apply

Vectorised operations

In this article we show how to perform complex numerical operations without loops.
For loops in Python are slow. If one needs to apply a mathematical operation on multiple (consecutive) elements of an array, it is always better to use a vectorised operation if possible.

In practice, a vectorised operation means reframing the code in a manner that completely avoids a loop and instead uses e.g. slicing to apply the operation on the whole array (slice) at one go.

For example, the following code for calculating the difference of neighbouring elements in an array:

# brute force using a for loop
arr = numpy.arange(1000)
dif = numpy.zeros(999, int)
for i in range(1, len(arr)):
dif[i-1] = arr[i] - arr[i-1]

can be re-written as a vectorised operation:

# vectorised operation
arr = numpy.arange(1000)
dif = arr[1:] - arr[:-1]

Vectorized operation

Try to measure the for -loop version and vectorize version e.g. with timeit with different array sizes. How large difference you do see in performance?

© CC-BY-NC-SA 4.0 by CSC - IT Center for Science Ltd.
This article is from the free online

Python in High Performance Computing

Created by
FutureLearn - Learning For Life