测试3种方式隔点取矩阵,操作耗时
直接赋值耗时最少几乎没有,深拷贝和浅拷贝耗时是相同的,我猜测直接赋值只是改变了索引参数,深拷贝和浅拷贝做了相同的操作
于是做进一步的实验,将三种方式得到的张量使用canny取边缘,三者得到的结果是相同的,但直接赋值的耗时是另外两者的2倍多,深拷贝和浅拷贝基本相同
img = cv2.imread("35.png")
t0 = time.time()
for i in range(1000):
img1_ = img[::2, ::2, :]
print(f'direct cost: {time.time() - t0}')
t0 = time.time()
for i in range(1000):
img2_ = copy.copy(img[::2, ::2, :])
print(f'copy cost: {time.time() - t0}')
t0 = time.time()
for i in range(1000):
img3_ = copy.deepcopy(img[::2, ::2, :])
print(f'deepcopy cost: {time.time() - t0}')
t0 = time.time()
for i in range(1000):
canny1 = cv2.Canny(img1_, 50, 80)
print(f'direct and canny cost: {time.time() - t0}')
t0 = time.time()
for i in range(1000):
canny2 = cv2.Canny(img2_, 50, 80)
print(f'copy and canny cost: {(time.time() - t0)}')
t0 = time.time()
for i in range(1000):
canny3 = cv2.Canny(img3_, 50, 80)
print(f'deepcopy and canny cost: {time.time() - t0}')
direct cost: 0.0010030269622802734
copy cost: 0.9650290012359619
deepcopy cost: 0.9601104259490967
direct and canny cost: 2.078744649887085
copy and canny cost: 0.8433835506439209
deepcopy and canny cost: 0.8281993865966797