Vectorization — Next level of using loops in Python
Python has become one of the most popular programming languages in the world today. Its ease of use and flexibility have made it an ideal choice for many applications. However, one of the most common issues that users face is the time it takes to execute code written in Python. This is due to the fact that Python is an interpreted language, meaning that code is executed line by line, instead of being compiled down to machine code and executed in one go. This can lead to code that is slow and clunky, and can make tasks that require large amounts of computation take an unacceptably long time.
The traditional way of dealing with this issue has been to make use of loops, which allow the programmer to iterate over a set of data and perform certain operations on each element in the set. While loops are an effective way of tackling these problems, they come with their own set of challenges. Loops can be difficult to read and debug, and can often lead to code that is unorganized and bloated. In addition, loops can be slow, as the code is executed multiple times for each element in the set.
Thankfully, there is a more efficient and powerful way to tackle these issues: vectorization. Vectorization is a method of programming that allows the user to perform operations on multiple sets of data at once, rather than looping over each element in the set individually. This can lead to code that is much more efficient and easier to read. In addition, vectorization can be much faster than loops, as the same operation is being performed on multiple elements simultaneously.
In Python, vectorization is made possible by the NumPy library. NumPy is a powerful library that provides the user with a variety of tools for working with multi-dimensional arrays. These tools allow the user to quickly and easily create, manipulate, and transform arrays of data. In addition, NumPy provides the user with a variety of mathematical functions that can be used to perform operations on the data in the arrays. This makes it possible to quickly and easily perform operations on multiple sets of data at once, without having to write complex loops.
So, if you are looking for a way to optimize your Python code and make it faster and easier to read, vectorization is the way to go. In addition to being more efficient and easier to read, vectorization can provide a much faster execution time than loops. So, if you are looking to speed up your Python code, vectorization is the way to go. So, say goodbye to loops and welcome vectorization into your Python code!
Try some demo code below:
Using Loops
import time
start = time.time()
# iterative sum
total = 0
# iterating through 1.5 Million numbers
for item in range(0, 1500000):
total = total + item
print('sum is:' + str(total))
end = time.time()
print(end - start)
#1124999250000
#0.14 Seconds
Using Vectors
import numpy as np
start = time.time()
# vectorized sum - using numpy for vectorization
# np.arange create the sequence of numbers from 0 to 1499999
print(np.sum(np.arange(1500000)))
end = time.time()
print(end - start)
##1124999250000
##0.008 Seconds