当前位置:网站首页>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()

边栏推荐
- 基于Lambert函数的时滞系统稳定性研究
- matlab图像去雾技术GUI界面-全局平衡直方图
- What is a firewall? What role can firewalls play?
- 【TA-霜狼_may-《百人计划》】图形3.4 延迟渲染管线介绍
- 降噪蓝牙耳机哪个好?性价比最高的降噪蓝牙耳机排行
- ReentrantLock 可重入锁
- 未来数据库需要关心的硬核创新
- Scala functions and their higher-order applications
- Withdrawal of IPO application, Yunzhou intelligent "tour" of unmanned boat enterprise fails to enter the science and Technology Innovation Board
- With this machine learning drawing artifact, papers and blogs can get twice the result with half the effort!
猜你喜欢

Join parameter processing and @param

You are only one SQL statement away from the tdengine Developer Conference!

After taking aiyouteng's medicine, Naifei's condition improved

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

Which is a good noise reduction Bluetooth headset? Ranking of the most cost-effective noise reduction Bluetooth headsets

Windows10安装免安装版redis

Mlx90640 infrared thermal imager temperature measurement module development notes (III)

未来数据库需要关心的硬核创新

Power of leetcode 231.2

Arduino IDE ESP32固件安装和升级教程
随机推荐
JUC source code learning note 3 - AQS waiting queue and cyclicbarrier, BlockingQueue
20. Shell programming variables
3、 Set foundation ArrayList set and simple student management system
[tf.keras]: a problem encountered in upgrading the version from 1.x to 2.x: invalidargumenterror: cannot assign a device for operation embedding_
Automatic derivation of pytorch
Lsyncd set up synchronous image - use lsyncd to realize real-time synchronization between local and remote servers
G026-db-gs-ins-03 openeuler deployment opengauss (1 active and 2 standby or multiple standby)
Dynamics crm: mailbox configuration (III) - configure email server profiles and mailboxes
Memcache cache application (lnmp+memcache)
应用修改日志路径log4j.properties
图像label处理——json文件转化为png文件
Configuring WAPI certificate security policy for Huawei wireless devices
Dynamics crm: how to set the order of forms
Do you understand the working principle of gyroscope?
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
每天20分钟之feign
Kubernetes版本对接对象存储
Yolov3 trains its own data set
Dynamics crm: [problem solved]cannot open SQL encryption symmetric key because symmetric key password
4279. Cartesian tree