Numpy底层使用C语言编写,内部解除了GIL(全局解释器锁),其对数组的操作速度不受Python解释器的限制,所以,其效率远高于纯Python代码
import cv2
import time
import os
import numpy as np
def read_img_and_show(file_path):
start = time.clock()
img = cv2.imread(file_path)
cv2.imshow('img', img)
elapsed = (time.clock() - start )
print(elapsed)
key = cv2.waitKey(0)
if key == 27:
return
def read_np_and_show(file_path):
start = time.clock()
img = np.load(file_path)
cv2.imshow('img', img)
elapsed = (time.clock() - start )
print(elapsed)
key = cv2.waitKey(0)
if key == 27:
return
if __name__ == '__main__':
read_img_and_show('test.jpg') # 0.05087599999999992
# img -> np
img = cv2.imread('test.jpg')
np.save('test.npy', img)
read_np_and_show('test.npy') # 0.005974000000000035 #时间相差了10倍