3

我想测量两行代码之间的时间差(以秒为单位)。

while ret:
    ret, image_np=cap.read()
    time_1
    for condition:
         if condition:
             time_2

我想减去(time_2) - (time_1)。但问题是它time_1总是在变化,我无法计算时间。

4

1 回答 1

3

您可以将值直接存储在数组中,并在每次将值附加到数组时更改 time_1 值。这是它的样子:

from datetime import datetime

time_1 = datetime.now()
elapsed_time = []

# In my example I loop from 0 to 9 and take the elapsed time
# when the value is 0 or 5
for i in range(10):
    if i in [0,5]:
        elapsed_time.append(datetime.now()-time_1)
        time_1 = datetime.now()
于 2019-08-26T13:16:58.343 回答