-1

I m watching the video of Vectorization in that author explain vectorization will reduce the time for computation. For example

Code:

import numpy as np
import time

a= np.random.rand(100000)
b= np.random.rand(100000)

tic = time.time()
c = np.dot(a,b)
toc = time.time()

print(c)
print("Vectorization Version: "+ str(100000 * (toc - tic)) +"ms" )

c = 0
tic = time.time()
for i in range(100000):
    c += a[i] * b[i]

toc = time.time()
print(c)
print("NonVectorization Version: "+ str(100000 * (toc - tic)) +"ms" )

Output:

25108.8250776
Vectorization Version: 12.660026550292969ms
25108.8250776
NonVectorization Version: 7782.268524169922ms

How vectorization are helpful in reducing the computation time?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
DRV
  • 676
  • 1
  • 8
  • 22
  • 1
    'vectorization' in the `numpy` context means 'using the compiled numpy methods and functions'. In this example `np.dot` does the job in a fast compiled BLAS library. The slow version iterates within `python` code, which is much slower (interpreted). – hpaulj Apr 17 '20 at 15:55

1 Answers1

0

Because Vectorization is SIMD (single instruction, multiple data) operation. One instruction carries out the same operation on a number of operands in parallel. [1]

While Loops usually refered as MIMD (multiple instruction, multiple data) operation by the computer. Even though there is only one operation in the loop, the computer sees these operations as different instructions at each iteration.