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')
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')