I'm not really proficient with cv2 but I do see something rather iffy with your timing technique here.
Look at what part of the code you are measuring with cv2.getTickCount():
e1 = cv2.getTickCount() # start
ret, frame = cap.read()
cv2.imshow("cam",frame)
e2 = cv2.getTickCount() # stop
Now, look at what you are measuring using time.time():
t_start=time.time() # start
time1=0
while True:
e1 = cv2.getTickCount()
ret, frame = cap.read()
cv2.imshow("cam",frame)
e2 = cv2.getTickCount()
time1 = (e2 - e1)/ cv2.getTickFrequency() + time1
elapsedTime= time.time()-t_start # stop
You are obviously mistreating poor ol' time over here by counting different things. Unfortunately, I cannot verify the actual runtimes because I do not have OpenCV, but, you might want to put your time calls on par with getTickCount(). In short, use something similar to this:
time1=0
while True:
t_start = time.time() # start
ret, frame = cap.read()
cv2.imshow("cam",frame)
elapsedTime= time.time()-t_start # stop
And re-evaluate your results, it's quite possible getTickCount() will get more accurate results than time because of the way it is implemented, I really can't know that.
As for which one should you use? cv2's internal timing module.
Why? Because, without making outlandish claims, it is a tested module that has probably been developed by people that are most likely more proficient than us with Python. Timing on your own can be a tricky thing and leaves a lot of room for little mistakes, especially when you're first starting out.
So, in short, go with getTickCount(), it is there for a reason.
I'm not really proficient with cv2 but I do see something rather iffy with your timing technique here.
Look at what part of the code you are measuring with cv2.getTickCount():
e1 = cv2.getTickCount() # start
ret, frame = cap.read()
cv2.imshow("cam",frame)
e2 = cv2.getTickCount() # stop
Now, look at what you are measuring using time.time():
t_start=time.time() # start
time1=0
while True:
e1 = cv2.getTickCount()
ret, frame = cap.read()
cv2.imshow("cam",frame)
e2 = cv2.getTickCount()
time1 = (e2 - e1)/ cv2.getTickFrequency() + time1
elapsedTime= time.time()-t_start # stop
You are obviously mistreating poor ol' time over here by counting different things. Unfortunately, I cannot verify the actual runtimes because I do not have OpenCV, but, you might want to put your time calls on par with getTickCount(). In short, use something similar to this:
time1=0
while True:
t_start = time.time() # start
ret, frame = cap.read()
cv2.imshow("cam",frame)
elapsedTime= time.time()-t_start # stop
And re-evaluate your results, it's quite possible getTickCount() will get more accurate results than time because of the way it is implemented, I really can't know that.
As for which one should you use? cv2's internal timing module.
Why? Because, without making outlandish claims, it is a tested module that has probably been developed by people that are most likely more proficient than us with Python. Timing on your own can be a tricky thing and leaves a lot of room for little mistakes, especially when you're first starting out.
So, in short, go with getTickCount(), it is there for a reason.
This should produce consistent results:
t_start = time.time()
e1 = cv2.getTickCount()
while True:
ret, frame = cap.read()
cv2.imshow("cam",frame)
e2 = cv2.getTickCount()
current_time = time.time()
time1 = (e2 - e1)/ cv2.getTickFrequency()
elapsedTime= current_time - t_start
print [time1 , elapsedTime]
Actually, I cannot reproduce your problem:
import time
import cv2
t_start = time.time()
e1 = cv2.getTickCount()
for x in range(10):
time.sleep(0.1)
e2 = cv2.getTickCount()
time1 = (e2 - e1)/ cv2.getTickFrequency()
elapsedTime= time.time() - t_start
print 'new:', [time1 , elapsedTime]
t_start=time.time()
time1=0
for x in range(10):
e1 = cv2.getTickCount()
time.sleep(0.1)
e2 = cv2.getTickCount()
time1 = (e2 - e1)/ cv2.getTickFrequency() + time1
elapsedTime= time.time()-t_start
print 'old:', [time1 , elapsedTime]
Pints rather consistent values:
new: [0.101121973, 0.10170483589172363]
new: [0.202431593, 0.20299196243286133]
new: [0.303379863, 0.3039419651031494]
new: [0.404390379, 0.40494799613952637]
new: [0.505434471, 0.5059988498687744]
new: [0.606657266, 0.6072208881378174]
new: [0.70785313, 0.7083899974822998]
new: [0.808160676, 0.808696985244751]
new: [0.90930442, 0.9098358154296875]
new: [1.010264773, 1.0107979774475098]
old: [0.101093147, 0.10112500190734863]
old: [0.201946665, 0.20209193229675293]
old: [0.302889158, 0.3032569885253906]
old: [0.40368225999999996, 0.40418291091918945]
old: [0.504733644, 0.5053050518035889]
old: [0.605701044, 0.6063768863677979]
old: [0.7066711290000001, 0.707442045211792]
old: [0.807242644, 0.8081610202789307]
old: [0.908066402, 0.9092309474945068]
old: [1.008913762, 1.0102128982543945]
Are you sure the printed numbers correspond to code you show in your question?
There is no room for confusion, all the references you have given point to the same thing.
getTickCount gives you the number of clock cycles after a certain event, eg, after machine is switched on.
A = getTickCount() // A = no. of clock cycles from beginning, say 100
process(image) // do whatever process you want
B = getTickCount() // B = no. of clock cycles from beginning, say 150
C = B - A // C = no. of clock cycles for processing, 150-100 = 50,
// it is obvious, right?
Now you want to know how many seconds are these clock cycles. For that, you want to know how many seconds a single clock takes, ie clock_time_period. If you find that, simply multiply by 50 to get total time taken.
For that, OpenCV gives second function, getTickFrequency(). It gives you frequency, ie how many clock cycles per second. You take its reciprocal to get time period of clock.
time_period = 1/frequency.
Now you have time_period of one clock cycle, multiply it with 50 to get total time taken in seconds.
Now read all those references you have given once again, you will get it.
dwStartTimer=GetTickCount();
dwEndTimer=GetTickCount();
while((dwEndTimer-dwStartTimer)<wDelay)//delay is 5000 milli seconds
{
Sleep(200);
dwEndTimer=GetTickCount();
if (PeekMessage (&uMsg, NULL, 0, 0, PM_REMOVE) > 0)
{
TranslateMessage (&uMsg);
DispatchMessage (&uMsg);
}
}