top of page

Vectorised and non vectorised code comparison in Python

vectorization is required in python when we are dealing with matrics. With the evolution of deep learning it has gained more lime-light.

Here is the execution time comparison of vectorised and non vectorised code.

# initialisation of array

import numpy as np


[1 2 3 4]

# initialise numpy array

import time
A = np.random.rand(1000000)
B = np.random.rand(1000000)
# calculating execution time using vectorization
tic = time.time()
C, B)
toc =time.time()
print('total time taken in vectorised multiplication' + str(toc-tic) + 'mili-seconds')

total time taken in vectorised multiplication 0.002000093460083008 mili-seconds

# calculating execution time using non vectorization codetic = time.time()
for i in range(1000000):
    C = C +A[i]*B[i]   
toc= time.time()
print('total time taken in non-vectorised code'+ str(toc-tic) +' mili seconds')

749296.5132501889 total time taken in non-vectorised code0.6779999732971191 mili seconds.

8 views0 comments

Recent Posts

See All

1. What is co-variance matrix- A covariance matrix is a square matrix that describes the covariance between multiple variables. It is commonly used in statistics and probability theory to describe the

bottom of page