在python-numpy
使用中,可以用双层 for循环对数组元素进行访问,也可以切片成每一行后进行一维数组的遍历。
代码如下:
import numpy as np
import time
NUM = 160
a=np.random.random((NUM,NUM))
start = time.time()
for i in range(NUM):
for j in range(NUM):
if a[i][j] == 1.0:
pass
end1 = time.time()
for ii in range(NUM):
b = a[ii,:]
for jj in range(NUM):
if b[jj] == 1.0:
pass
end2 = time.time()
print("end1",end1-start)
print("end2",end2-end1)
由于生成的是[0,1)中的数,因此两种操作会遍历所有的元素。多轮测试后,耗时如下:
当NUM为160时:
end1 0.006983518600463867
end2 0.003988742828369141
当NUM为1600时:
end1 0.71415114402771
end2 0.45178747177124023
结论:切片后遍历更快
原因:
楼主还暂不明确
一个想法:
b=a[ii,:]
在numpy中,为了提高效率,这种切片出来的子矩阵其实都是原矩阵的引用而已,所以改变子矩阵,原矩阵还是会变的
所以在内层循环中,第二种方法是在那一行元素所在的内存进行寻找。而第一种方法是先定位到行,再定位到列,所以比较慢?
大家是怎么想的呢?
关于numba
在小数据量下的速度慢于普通操作
什么是numba?
numba
实验比较:
import numpy as np
import time
NUM = 160
from numba import jit
a=np.random.random((NUM,NUM))
@jit(nopython=True)
def fun1(a):
for i in range(NUM):
for j in range(NUM):
if a[i][j] == 1.0:
pass
def fun2(a):
for i in range(NUM):
for j in range(NUM):
if a[i][j] == 1.0:
pass
@jit(nopython=True)
def fun3(a):
for ii in range(NUM):
b = a[ii,:]
for jj in range(NUM):
if b[jj] == 1.0:
pass
def fun4(a):
for iii in range(NUM):
b = a[iii,:]
for jjj in range(NUM):
if b[jjj] == 1.0:
pass
start = time.time()
fun1(a)
end1 = time.time()
fun2(a)
end2 = time.time()
fun3(a)
end3 = time.time()
fun4(a)
end4 = time.time()
print("end1",end1-start)
print("end2",end2-end1)
print("end3",end3-end2)
print("end4",end4-end3)
首先,当NUM为1600时,结果如下:
end1 0.2991981506347656 #无切片,有加速
end2 0.6372940540313721 #无切片,无加速
end3 0.08377814292907715 #有切片,有加速
end4 0.358079195022583 #有切片,无加速
其他条件相同的情况下,有切片的速度更快。同样,有numba加速的也比没加速的快。
但当NUM =160时,结果如下:
end1 0.29620814323425293 #无切片,有加速
end2 0.006980180740356445 #无切片,无加速
end3 0.08580684661865234 #有切片,有加速
end4 0.0029993057250976562 #有切片,无加速
有切片依旧比无切片的快。但是有numba加速的却比没有numba加速的慢。
原来@jit(nopython=True)只是对函数进行修饰,第一次调用会进行编译,编译成机器码,之后速度就会很快。
实验代码如下:
import numpy as np
import time
NUM = 160
from numba import jit
a=np.random.random((NUM,NUM))
@jit(nopython=True)
def fun1(a):
for i in range(NUM):
for j in range(NUM):
if a[i][j] == 1.0:
pass
def fun2(a):
for i in range(NUM):
for j in range(NUM):
if a[i][j] == 1.0:
pass
@jit(nopython=True)
def fun3(a):
for ii in range(NUM):
b = a[ii,:]
for jj in range(NUM):
if b[jj] == 1.0:
pass
def fun4(a):
for iii in range(NUM):
b = a[iii,:]
for jjj in range(NUM):
if b[jjj] == 1.0:
pass
for b in range(4):
start = time.time()
fun1(a)
end1 = time.time()
fun2(a)
end2 = time.time()
fun3(a)
end3 = time.time()
fun4(a)
end4 = time.time()
print("end1",end1-start)
print("end2",end2-end1)
print("end3",end3-end2)
print("end4",end4-end3)
print("---")
结果如下:
end1 0.29421305656433105
end2 0.0059833526611328125
end3 0.08181905746459961
end4 0.0029909610748291016
---
end1 0.0
end2 0.005949735641479492
end3 0.0
end4 0.004008769989013672
---
end1 0.0
end2 0.006977558135986328
end3 0.0
end4 0.00399017333984375
---
end1 0.0
end2 0.005974292755126953
end3 0.0
end4 0.003837108612060547
---
结论:
numba
加速时,第一次需要编译,需要耗时。之后调用就不需要了。
到此这篇关于python中的Numpy二维数组遍历与二维数组切片后遍历效率比较的文章就介绍到这了,更多相关Numpy二维数组遍历与二维数组切片后遍历效率比较内容请搜索易知道(ezd.cc)以前的文章或继续浏览下面的相关文章希望大家以后多多支持易知道(ezd.cc)!