DEV Community

SK RAJIBUL
SK RAJIBUL

Posted on

Loops and Vectorization in Python

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


Enter fullscreen mode Exit fullscreen mode

Using Vectorization:

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

Enter fullscreen mode Exit fullscreen mode

Vectorization took ~18x less time to execute as compared to the iteration using the range function.

This difference will become more significant while working with Pandas DataFrame.

Top comments (0)