Vecotrization

Why vectorization is important than using the explicit for loops,


In [1]:
# Importing necessary modules
import numpy as np
import time

In [2]:
# Getting a random 1 million dimensional matrix
a = np.random.rand(100000)
# Getting another random matrix
b = np.random.rand(100000)

In [3]:
# Calculating the start time
tic = time.time()

# Calculating the product (which is one of the computation in computation graph)
c = np.dot(a,b)

# calculating the end time
toc = time.time()

print('For Vectorization , c = ', c, ' time=', 10000*(toc-tic), 'ms')


For Vectorization , c =  25018.917232656837  time= 10.018348693847656 ms

In [4]:
# Re-initializing to 0
c = 0

In [5]:
# By using explict for loop
# start time
tic = time.time()
for i in range(100000):
    # Calculating the cumilative value of c
    c += a[i] * b[i]
# end time
toc = time.time()

print('Using explicit for loop, c = ', c, ' time=', 10000*(toc-tic), 'ms')


Using explicit for loop, c =  25018.917232656648  time= 1040.7543182373047 ms

Observation

We can see the for calculating the same value using explit for loop took lot of more time compared to time taken by using the Vectorization, this is why using vectorization is very important in using Neural Networks.