The world’s leading publication for data science, AI, and ML professionals.

How Much Time Does NumPy Save You?

Insight into the speed benefit of NumPy functions

Photo by Florian Steciuk on Unsplash
Photo by Florian Steciuk on Unsplash

NumPy is a very well known library for doing mathematical computations with python. It is especially used in the field of data science and machine learning.

NumPy gives you access to a lot of mathematical functions and Algorithms. This saves you a lot of development time as you do not have to implement them yourself, but is there also computational time to be saved using this library? This post includes some small algorithmic experiments that help us answer this question.

Lets dig in!


NumPy arrays vs lists

Let’s start out by taking a look at Numpy arrays and compare them to the vanilla python list.

Both lists and NumPy arrays can be used to represent vectors.

import numpy as np
list_a = [1, 2, 3, 4]
array_a = np.array([1, 2, 3, 4])

However, the two different data structures have very different behaviors. For example, when applying a function to them.

list_a + list_a
>> [1, 2, 3, 4, 1, 2, 3, 4]
array_a + array_a
>> array([2, 4, 6, 8])

Two vanilla lists added together result in a new list that is a concatenation of the two. Whereas the two arrays summed together applies the addition element wise.

This is also apparent in the following example:

list_a + 5
>> TypeError: can only concatenate list (not "int") to list
array_a + 5
>> array([6, 7, 8, 9])

Where attempting to add 5 to the list raises a TypeError because it is trying to concatenate an integer to the list. On the other hand, when adding 5 to the array it is applied for each element, thus returning a new array where all the elements of the previous array have been increased by 5.

Those are some of the basic differences between lists and NumPy arrays which are important to be aware of when deciding what to use in your use case.


Speed comparisons

Dot product calculations Next lets take a deeper look into the speed benefit that can be achieved when using NumPy’s functions on arrays instead of using hand implemented functions.

Using NumPy’s randnfunction we can quickly populate an array with 100 random numbers.

Using datetime.now() we get the current time before and after we run the hand implemented function 100,000 times.

And we use the same approach when using NumPy’s dot() function.

Lastly we print the difference between the two times as a ratio, and we can see that the hand implemented function is about 65 times slower than NumPy’s.

I have run this code snippet a couple of times, and the result varies a bit between a ratio of 55 and 65. I encourage you to try it out yourself if you like.


Matrix multiplicationsLet’s take a look at another speed comparison. This time we will analyze matrix multiplications.

In the following code snippet you will find my implementation of a matrix multiplication algorithm, and just like in the previous example it will be compared to the built-in dot function in NumPy.

Code walk through Cell 1: Importing the necessary libraries.

Cell 2: Creating 3 different matrices. A is a 3×2, B is a 2×3, and C is a 5×2 matrix.

Cell 3: Defining a matrix multiplication function.

Cell 4: Checking that the hand implemented function returns the same result as NumPy does.

Cell 5: Timing the functions when dotting a 2×3 matrix with a 3×2 matrix 100,000 times, and finding the ratio between them.

Cell 6: Timing the functions when dotting a 5×2 matrix with a 2×3 matrix 100,000 times, and finding the ratio between them.

The results The results show that A dotted with B is about 23 times faster when using NumPy compared to my own function.

And that C dotted with A is about 49 times faster using the NumPy function. This observation also indicates that when the size of the matrices grow then the the time that is saved by using NumPy also increases by a large factor.

As these results reveal, there is a pretty huge time benefit in using NumPy. You are probably using it anyway. If you are a curious person like me, then you probably enjoyed this little experiment, and learned that your favorite python math library is even better than you thought.


Credits go to The Lazy Programmer, as it was through his material that I became aware of this.

Thanks for reading!


Related Articles