1

enter image description hereI am providing a piece of code of project for iris localization. So here I would like to get amount of time that a person spends looking at a particular direction. So moreover I want to get entire time a code spends in a specific if-elif construct.

I have tried something using timeit module.But I was not able to get the time when the code enters the if section and the time at which it leaves the while loop.

                thresh = (five_eye[1]-one_eye[1])
                print("thresh:",thresh)
                # if 13<thresh <16:
                #     print('CENTER')
                #     cv2.putText(frameClone, 'CENTER', 
(90,40),cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 2)
                #     single_eye_state.append(index)
                #     single_eye_state.append('CENTER')
                if 10<thresh<=13:
                    start = timeit.timeit()
                    print('Looking Down 1 Feet')
                    cv2.putText(frameClone,'Looking Down 1 Feet', 
(90,40),cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 2)
                    single_eye_state.append(index)
                    single_eye_state.append('DOWN1')
                    end = timeit.timeit()
                    print(end - start)
                elif 7<thresh<=10:
                    start = timeit.timeit()
                    print("Looking Down 2 Feet")
                    cv2.putText(frameClone,'Looking Down 2 Feet', 
(90,40),cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 2)
                    single_eye_state.append(index)
                    single_eye_state.append('DOWN2')
                    end = timeit.timeit()
                    print(end - start)

Need the total time a code spends in a while loop.

  • [This](https://stackoverflow.com/questions/1557571/how-do-i-get-time-of-a-python-programs-execution) should help you time your function. Also you are not calculating the spent time in your elif, so you wont get any output if you end in the elif part. – Kevin Müller Oct 04 '19 at 05:47

1 Answers1

2

Only one of the section will run, either if or elif. So it is better to just wrap the if-elif construct and get the time taken like:

start = timeit.timeit()
if condition:
    ....
elif:
    ....
end = timeit.timeit()
print(end-start)

In case you want total time over multiple sections:

total = 0
if condition:
    start = timeit.timeit()
    ....
    end = timeit.timeit()
    total += end - start
elif:
    start = timeit.timeit()
    ....
    end = timeit.timeit()
    total += end - start
print(total)

Note: Only one of the section will run i.e, either if on elif. I added the code just for clarity.

Yash
  • 3,438
  • 2
  • 17
  • 33
  • Thank you Yash. Actually I would like to get the time in every conditions, Just now I have made some changes so check it out my code once again and I will expect your valuable reply. – Adithya Raj Oct 04 '19 at 09:58
  • Your edited code looks ok to me, what problem are you facing with it? – Yash Oct 04 '19 at 13:43
  • Now it is printing just printing the execution time of two statements in the if condition. So just imagine a person is looking down 1 feet for a duration of 10 seconds so the first if condition should print 10 sec, but now it is printing the execution time every instant but I want the overall duration. I will provide you some screenshots. Thank you Yash once again for your comment. – Adithya Raj Oct 05 '19 at 05:02
  • 1. if it is just running in the if statement it is because only the if condition is running which is the natural way. 2. For overall time, I have already hinted in my code – Yash Oct 05 '19 at 06:34
  • Great.. Thank you! – Adithya Raj Oct 05 '19 at 10:11
  • Yah... I mean it's working and I am able to get the time at all instants, so what I am going to try is taking the first time and the final time and calculate the duration. Can you give any hint for it, like should I append the times to a list or not, else whatever it may be. – Adithya Raj Oct 07 '19 at 07:21
  • 1
    Oh now I get it. Instead of using list, you can simply calculate the time in each section and add it to the total time. I have updated my answer with sample – Yash Oct 07 '19 at 09:52