为什么这个Python程序运行得比我预期的慢?

为什么这个Python程序运行得比我预期的慢?,python,arrays,numpy,opencv,path,Python,Arrays,Numpy,Opencv,Path,我有两个测试程序都拍了一张照片,但其中一个使用numpy的zeros()函数将其作为临时RGB数据写入。而另一个使用cv2的.imwrite()模块写入磁盘。因此,我的问题是,对于我的使用,RGB版本显然是更好的方式,因此我可以避免操纵路径和磁盘内存,但我的争论是,两个程序的运行速度几乎相同。我相信,当将数组分配给变量时,程序应该运行得更快,而不是写入驱动器并操纵其文件路径。我是做错了什么,还是做了一些奇怪的非正统的事情,只是碰巧很慢?这个程序拍得越快,对我来说就越好。再次感谢您的建议 RGB版

我有两个测试程序都拍了一张照片,但其中一个使用numpy的
zeros()
函数将其作为临时RGB数据写入。而另一个使用cv2的
.imwrite()
模块写入磁盘。因此,我的问题是,对于我的使用,RGB版本显然是更好的方式,因此我可以避免操纵路径和磁盘内存,但我的争论是,两个程序的运行速度几乎相同。我相信,当将数组分配给变量时,程序应该运行得更快,而不是写入驱动器并操纵其文件路径。我是做错了什么,还是做了一些奇怪的非正统的事情,只是碰巧很慢?这个程序拍得越快,对我来说就越好。再次感谢您的建议

RGB版本

start = timeit.default_timer()
import numpy as np 
import cv2
def Array_Test():

    hud_Cam = cv2.VideoCapture(0, cv2.CAP_DSHOW) 

    #If Hud camera is not found
    if not hud_Cam.isOpened():
        #Log error to console
        raise RuntimeError('Error opening Hud Camera.')
        
    #Set resolution of camera to 720p
    hud_Cam.set(3, 1280)
    hud_Cam.set(4, 720)
    
    while True:
        #Read/Grab Hud camera frame
        rval, hud_Img = hud_Cam.read()
        hud_Cam.release()
        snapshot = np.zeros(hud_Img.shape, dtype=np.uint8)
        snapshot = hud_Img.copy()
        print(snapshot)
        break
Array_Test()
stop = timeit.default_timer()
print('Program Timer: ', stop - start)
import timeit
start = timeit.default_timer()
import cv2
import os

#Call Hud camera resource
hud_Cam = cv2.VideoCapture(0, cv2.CAP_DSHOW) 
#If Hud camera is not found
if not hud_Cam.isOpened():
    #Log error to console
    raise RuntimeError('Error opening Hud Camera.')
    hud_Cam.release()
def _HudCam_(hud_Cam): #Define Hud Camera Function

    #Set resolution of camera to 720p
    hud_Cam.set(3, 1280)
    hud_Cam.set(4, 720)

    #Read/Grab Hud camera frame
    rval, hud_Img = hud_Cam.read()
        
    #Initialize file name
    hud_name = ('C:/Users/Logan/Downloads/HudPicture-0.jpg') 

    #Write Hud camera frame to file name
    cv2.imwrite(hud_name, hud_Img) 

    #Initialize rename hud jpg variable
    old_file1 = os.path.join("C:/Users/Logan/Downloads", "HudPicture-0.jpg") 

    #Initialize rename hud jpg variable
    new_file1 = os.path.join("C:/Users/Logan/Downloads", "HudPicture0.jpg") 

    #If a file exists under the renamed files path
    if (os.path.exists('C:/Users/Logan/Downloads/HudPicture0.jpg')):
        #Delete the file in the path
        os.remove('C:/Users/Logan/Downloads/HudPicture0.jpg')

    #Rename file so labview knows its not being written to
    os.rename(old_file1, new_file1) 

    #Log to console
    print("Hud Picture written!") 

    #Release camera resource to clear memory
    hud_Cam.release() 
_HudCam_(hud_Cam)
stop = timeit.default_timer()
print(stop - start)
磁盘版本

start = timeit.default_timer()
import numpy as np 
import cv2
def Array_Test():

    hud_Cam = cv2.VideoCapture(0, cv2.CAP_DSHOW) 

    #If Hud camera is not found
    if not hud_Cam.isOpened():
        #Log error to console
        raise RuntimeError('Error opening Hud Camera.')
        
    #Set resolution of camera to 720p
    hud_Cam.set(3, 1280)
    hud_Cam.set(4, 720)
    
    while True:
        #Read/Grab Hud camera frame
        rval, hud_Img = hud_Cam.read()
        hud_Cam.release()
        snapshot = np.zeros(hud_Img.shape, dtype=np.uint8)
        snapshot = hud_Img.copy()
        print(snapshot)
        break
Array_Test()
stop = timeit.default_timer()
print('Program Timer: ', stop - start)
import timeit
start = timeit.default_timer()
import cv2
import os

#Call Hud camera resource
hud_Cam = cv2.VideoCapture(0, cv2.CAP_DSHOW) 
#If Hud camera is not found
if not hud_Cam.isOpened():
    #Log error to console
    raise RuntimeError('Error opening Hud Camera.')
    hud_Cam.release()
def _HudCam_(hud_Cam): #Define Hud Camera Function

    #Set resolution of camera to 720p
    hud_Cam.set(3, 1280)
    hud_Cam.set(4, 720)

    #Read/Grab Hud camera frame
    rval, hud_Img = hud_Cam.read()
        
    #Initialize file name
    hud_name = ('C:/Users/Logan/Downloads/HudPicture-0.jpg') 

    #Write Hud camera frame to file name
    cv2.imwrite(hud_name, hud_Img) 

    #Initialize rename hud jpg variable
    old_file1 = os.path.join("C:/Users/Logan/Downloads", "HudPicture-0.jpg") 

    #Initialize rename hud jpg variable
    new_file1 = os.path.join("C:/Users/Logan/Downloads", "HudPicture0.jpg") 

    #If a file exists under the renamed files path
    if (os.path.exists('C:/Users/Logan/Downloads/HudPicture0.jpg')):
        #Delete the file in the path
        os.remove('C:/Users/Logan/Downloads/HudPicture0.jpg')

    #Rename file so labview knows its not being written to
    os.rename(old_file1, new_file1) 

    #Log to console
    print("Hud Picture written!") 

    #Release camera resource to clear memory
    hud_Cam.release() 
_HudCam_(hud_Cam)
stop = timeit.default_timer()
print(stop - start)

使用a查找每个版本中的瓶颈。还有,如果在这个while循环的末尾有一个
break
语句,为什么会有一个while循环呢?idk lol,但我的意思是它没有任何我不认为的东西,是吗?什么是档案我的评论中有一个链接。我去掉了while循环。我不记得我为什么会有它。我想我是用它来循环cv2.imshow()以在0.000秒内测试214个函数调用(207个基元调用)时保持窗口打开