Experience the good calculation efficiency of vectorization in Python

Why vectorization is needed

When there are w and x of n-dimensional vector, it is inefficient if the for statement is used to calculate $ z = w_1x_1 + w_2x_2 ... w_nx_n $. When vectorized, it can be calculated efficiently by setting $ z = w ^ Tx + b $. In Python, you can compare with the following code.

import numpy as np
import time

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

tic = time.time()
c = np.dot(a, b)
toc = time.time()
print(f'{"Vectrized version":20}:{str(1000 * (toc-tic))} ms')

c = 0
tic = time.time()
for i in range(1000000):
    c += a[i] * b[i]
toc = time.time()
print(f'{"For loop":20}:{str(1000 * (toc-tic))} ms')
Vectrized version   :3.9501190185546875 ms
For loop            :1007.7228546142578 ms

With exponential calculation

Suppose you want to perform an exponential operation on a 1000000 dimensional vector $ v $.

import numpy as np
import time
import math

v = np.random.rand(1000000)
u = np.zeros((1000000, 1))
tic = time.time()
u = np.exp(v)
toc = time.time()
print(f'{"Vectrized version":20}:{str(1000 * (toc-tic))} ms')

c = 0
tic = time.time()
for i in range(1000000):
    u[i] = math.exp(v[i])
toc = time.time()
print(f'{"For loop":20}:{str(1000 * (toc-tic))} ms')
Vectrized version   :3.992319107055664 ms
For loop            :857.1090698242188 ms

Summary

There is a difference of about 300 times between the vectorized version and the non-vectorized version. When I want to use for-loop, I want to think of a way to avoid using it.

Recommended Posts

Experience the good calculation efficiency of vectorization in Python
Check the behavior of destructor in Python
I compared the calculation time of the moving average written in Python
The result of installing python in Anaconda
The basics of running NoxPlayer in Python
In search of the fastest FizzBuzz in Python
Output the number of CPU cores in Python
[Python] Sort the list of pathlib.Path in natural sort
Get the caller of a function in Python
Match the distribution of each group in Python
View the result of geometry processing in Python
Calculation result after the decimal point in Python
Make a copy of the list in Python
Find the divisor of the value entered in python
Find the solution of the nth-order equation in python
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Output in the form of a python array
the zen of Python
Date calculation in python
Date calculation in Python
How to get the number of digits in Python
Calculation of standard deviation and correlation coefficient in Python
[python] Get the list of classes defined in the module
The story of FileNotFound in Python open () mode ='w'
[python] Calculation of months and years of difference in datetime
Learn the design pattern "Chain of Responsibility" in Python
Implement the solution of Riccati algebraic equations in Python
Get the size (number of elements) of UnionFind in Python
Not being aware of the contents of the data in python
Reproduce the execution example of Chapter 4 of Hajipata in Python
Let's use the open data of "Mamebus" in Python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Heapsort)
[Python] Outputs all combinations of elements in the list
Get the URL of the HTTP redirect destination in Python
A reminder about the implementation of recommendations in Python
Reproduce the execution example of Chapter 5 of Hajipata in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Check the asymptotic nature of the probability distribution in Python
Towards the retirement of Python2
Download the file in Python
Find the difference in Python
Shapley value calculation in Python
About the ease of Python
Equivalence of objects in Python
Implementation of quicksort in Python
About the features of Python
The Power of Pandas: Python
Try scraping the data of COVID-19 in Tokyo with Python
Find out the apparent width of a string in python
I tried the accuracy of three Stirling's approximations in python
[Python] Heron's formula functionalization and calculation of the maximum area
Measure the execution result of the program in C ++, Java, Python.
Check the operation of Python for .NET in each environment
[Memo] The mystery of cumulative assignment statements in Python functions
The result of Java engineers learning machine learning in Python www
Calculate the square root of 2 in millions of digits with python
Have the equation graph of the linear function drawn in Python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Bubble Sort)