当前位置:网站首页>Research on the efficiency of numpy array access
Research on the efficiency of numpy array access
2022-07-24 15:51:00 【Fenghua Mingyuan】
numpy Array access research
1. brief introduction
numpy How to access arrays is more efficient ? Use numpy Methods provided , Or custom methods ? If the array is large , There is no doubt about using numpy The method provided is the most efficient . This paper studies search numpy Maximum 、 minimum value 、 arithmetic 、 Specific value search, etc 4 In this case .
2. Maximum and minimum
numpy Provides methods for maximum and minimum array , Namely max and min. Arrays are one-dimensional , from 1 Elements until 500 Elements . Arrays are randomly generated , The number of lookups is 1000 Time .
The code is as follows :
#coding:utf8
import numpy as np
import timeit
import matplotlib.pyplot as plt
def my_max(H):
r = -9223372036854775808 # int64 minimum value
for i in range(H.shape[0]):
if H[i]>r:
r = H[i]
return r
def my_min(H):
r = 9223372036854775807 # int64 Maximum
for i in range(H.shape[0]):
if H[i]<r:
r = H[i]
return r
def np_max(H):
return np.max(H)
def np_min(H):
return np.min(H)
if __name__ == '__main__':
array = 300
H = np.random.randint(100, 1000000, array)
x = np.arange(1,array+1)
npmin = []
mymin = []
npmax = []
mymax = []
times = 1000
for i in range(1,array+1):
K = H[0:i]
npmin.append(timeit.timeit("np_min(K)", setup="from __main__ import np_min",number=times,globals=globals()))
mymin.append(timeit.timeit("my_min(K)", setup="from __main__ import my_min", number=times, globals=globals()))
npmax.append(timeit.timeit("np_max(K)", setup="from __main__ import np_min",number=times,globals=globals()))
mymax.append(timeit.timeit("my_max(K)", setup="from __main__ import my_min", number=times, globals=globals()))
plt.plot(x,npmin,color='red')
plt.plot(x,mymin,color='green')
plt.plot(x,npmax, color='blue')
plt.plot(x,mymax, color='black')
plt.show()

Conclusion :
(1)numpy Methods o(1) Of . It doesn't grow linearly with the increase of array elements
(2) The custom method is o(n) Of
(3) Self defined max Method ratio min The method should be a little faster . It is estimated that the number of exchanges is less .
3. arithmetic
The four operations include numpy Four operations of array and constant , as well as numpy Array is the same as shape Of numpy Four operations of array .
3.1 numpy Four operations of array and constant
And find the maximum 、 The minimum value is similar , use numpy The self-contained method is more efficient in four operations . Copying arrays in code is not necessary , It depends on the actual implementation .
The code implements addition and multiplication respectively , Because there is no difference in the implementation method between subtraction and division and addition and multiplication .
#coding:utf8
import numpy as np
import timeit
import matplotlib.pyplot as plt
def my_add(K,value):
H = np.array(K)
for i in range(H.shape[0]):
H[i] += value
return H
def my_times(K,value):
H = np.array(K)
for i in range(H.shape[0]):
H[i] *= value
return H
def np_add(K,value):
H = np.array(K)
return H+value
def np_times(K,value):
H = np.array(K)
return H*value
if __name__ == '__main__':
array = 300
H = np.random.random(array)
x = np.arange(1,array+1,dtype=np.uint16)
times = 1000
npadd=[]
myadd=[]
nptimes=[]
mytimes=[]
for i in range(1, array + 1):
K = H[0:i]
npadd.append(timeit.timeit("np_add(K,20.)", setup="from __main__ import np_add", number=times, globals=globals()))
myadd.append(timeit.timeit("my_add(K,20.)", setup="from __main__ import my_add", number=times, globals=globals()))
nptimes.append(timeit.timeit("np_times(K,20.)", setup="from __main__ import np_times",number=times,globals=globals()))
mytimes.append(timeit.timeit("my_times(K,20.)", setup="from __main__ import my_times", number=times, globals=globals()))
plt.plot(x,npadd,color='red')
plt.plot(x,myadd,color='green')
plt.plot(x,nptimes, color='blue')
plt.plot(x,mytimes, color='black')
plt.show()

3.2 Array and four operations of array
There is little difference in the time consumption of four operations between arrays and between arrays and constants . This is because the time consumption is mainly reflected in the array loop , Not in four operations .
# coding:utf8
import numpy as np
import timeit
import matplotlib.pyplot as plt
def my_add(K, value):
H = np.array(K)
for i in range(H.shape[0]):
H[i] += value[i]
return H
def my_times(K, value):
H = np.array(K)
for i in range(H.shape[0]):
H[i] *= value[i]
return H
def np_add(K, value):
H = np.array(K)
return H + value
def np_times(K, value):
H = np.array(K)
return H * value
if __name__ == '__main__':
array = 300
H = np.random.random(array)
V = np.random.random(array)
x = np.arange(1, array + 1, dtype=np.uint16)
times = 1000
npadd = []
myadd = []
nptimes = []
mytimes = []
for i in range(1, array + 1):
K = H[0:i]
W = V[0:i]
npadd.append(
timeit.timeit("np_add(K,W)", setup="from __main__ import np_add", number=times, globals=globals()))
myadd.append(
timeit.timeit("my_add(K,W)", setup="from __main__ import my_add", number=times, globals=globals()))
nptimes.append(
timeit.timeit("np_times(K,W)", setup="from __main__ import np_times", number=times, globals=globals()))
mytimes.append(
timeit.timeit("my_times(K,W)", setup="from __main__ import my_times", number=times, globals=globals()))
plt.plot(x, npadd, color='red')
plt.plot(x, myadd, color='green')
plt.plot(x, nptimes, color='blue')
plt.plot(x, mytimes, color='black')
plt.show()

4. Specific value lookup
Specific value lookup is to find the specified value in the array .
# coding:utf8
import numpy as np
import timeit
import matplotlib.pyplot as plt
def my_find(K, value):
for i in range(K.shape[0]):
if K[i] == value:
return i
return -1
def np_find(K, value):
return np.where(K == value)[0]
if __name__ == '__main__':
array = 300
H = np.random.random(array)
V = np.random.random(1)
x = np.arange(1, array + 1, dtype=np.uint16)
times = 1000
npfind = []
myfind = []
for i in range(1, array + 1):
K = H[0:i]
npfind.append(
timeit.timeit("np_find(K,V[0])", setup="from __main__ import np_find", number=times, globals=globals()))
myfind.append(
timeit.timeit("my_find(K,V[0])", setup="from __main__ import my_find", number=times, globals=globals()))
plt.plot(x, npfind, color='red')
plt.plot(x, myfind, color='blue')
plt.show()

边栏推荐
- Yolov3 trains its own data set
- faster-rcnn 训练自己的数据集
- What is the ranking of good securities companies? Is online account opening safe
- 若依 this.$router.push 同地址不同参,页面不刷新问题
- YOLO5Face:为什么要重新发明人脸检测器
- After taking aiyouteng's medicine, Naifei's condition improved
- Research on stability of time-delay systems based on Lambert function
- [shaders realize pixelate mosaic effect _shader effect Chapter 7]
- kubernetes GPU的困境和破局
- 未来数据库需要关心的硬核创新
猜你喜欢

torch_ How to use scatter. Scatter() in detail

Join parameter processing and @param
![[SWT] user defined data table](/img/bf/a0c60f1ac9461874b8a573f805e1fe.png)
[SWT] user defined data table

AttributeError: module ‘seaborn‘ has no attribute ‘histplot‘

你不能只会flex居中布局,精制动画讲解所有flex布局方式!通俗易懂纯干货教程!...

There are more than 20 categories of the four operators in MySQL. It took three days and three nights to sort them out. Don't collect them quickly

狗牙根植物介绍

Simplified understanding: publish and subscribe

Dynamics crm: sharing records for users and teams

【SWT】自定义数据表格
随机推荐
Arduino ide esp32 firmware installation and upgrade tutorial
kubernetes GPU的困境和破局
Leetcode 223. rectangular area
C - partial keyword
Kubernetes version docking object storage
微调LayoutLM v3进行票据数据的处理和内容识别
iptables常用命令小清单
Yolov6 trains its own data set
Nine key measures to maintain server security in Hong Kong
Azure key vault (1) Introduction
Dynamics crm: how to set the order of forms
With this machine learning drawing artifact, papers and blogs can get twice the result with half the effort!
JUC源码学习笔记3——AQS等待队列和CyclicBarrier,BlockingQueue
Dynamics 365: how to get the threshold value of executemullerequest in batch requests
LaneATT
Is it safe for Huatai Securities to open a mobile account and will it be leaked
yolov3 训练自己的数据集
图像label处理——json文件转化为png文件
Memcache cache application (lnmp+memcache)
Introduction to kettle messy notes