Why can significantly speed up the quantification?

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Why can significantly speed up the quantification?

Andrew Ng modeled on the example of the teacher in the classroom:

import numpy as np
import time

a = np.random.rand(1000000)
b = np.random.rand(1000000)
tic = time.time()
c1 = np.dot(a,b)
toc = time.time()
print(c1)
print("Vectorrized version:"+str(1000*(toc-tic))+"ms")

tic = time.time()
c2 = 0
for i in range(1000000):
    c2 += a[i]*b[i]
toc = time.time()

print(c2)
print("For loop:"+str(1000*(toc-tic))+"ms")

Output:

250374.14909359306
Vectorrized version:0.9965896606445312ms
250374.14909359868
For loop:461.7724418640137ms

400 times faster than the vector calculation cycle, and why?

  • Python is an interpreted language, which means that your instructions to analyze and interpret each execution. Because they are not statically typed, so the cycle type of the operand must be evaluated at each iteration, which results in computational overhead.
  • An instruction to the quantization can perform the same operation in parallel (the SIMD (Single Instruction, Multiple Data) operation) of the plurality of operands. For example, to the size of a vector by a scalar N, let us call the M operand size can operate simultaneously. If so, then it needs to perform the number of instructions is approximately N / M, if the cyclic mode, the operation must be performed N times.

Guess you like

Origin blog.csdn.net/houhuipeng/article/details/93139578