当前位置:   article > 正文

测试直接赋值,深拷贝,浅拷贝隔点取矩阵操作耗时

测试直接赋值,深拷贝,浅拷贝隔点取矩阵操作耗时

测试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}')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/616612
推荐阅读
相关标签
  

闽ICP备14008679号