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.

Answer from Dimitris Fasarakis Hilliard on Stack Overflow
🌐
Shimat
shimat.github.io › opencvsharp_docs › html › 89004a16-7569-04fe-ed11-5f3bc461e1cf.htm
Cv2.GetTickCount Method
Returns the number of ticks. The function returns the number of ticks after the certain event (for example, when the machine was turned on).
🌐
ProjectPro
projectpro.io › recipes › what-is-use-of-gettickcount-and-gettickfrequency-functions
Use of the getTickCount and getTickFrequency functions -
February 20, 2023 - To calculate the time a block of code takes to execute, we can calculate the number of clock signals using cv2.getTickCount() at the beginning and the end of the block of code and divide its difference by the frequency, which can be obtained ...
Top answer
1 of 2
3

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.

2 of 2
0

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?

🌐
ProgramCreek
programcreek.com › python › example › 70433 › cv2.getTickCount
Python Examples of cv2.getTickCount
def make_h5py(): x_train_paths, y_train_paths = get_data('train') x_val_paths, y_val_paths = get_data('val') x_test_paths, y_test_paths = get_data('test') h5py_file = h5py.File(os.path.join(dir_path, 'data.h5'), 'w') start = cv2.getTickCount() write_data(h5py_file, 'train', x_train_paths, y_train_paths) time = (cv2.getTickCount()-start)/cv2.getTickFrequency() print ('parsing train data, Time:%.3fs'%time) start = cv2.getTickCount() write_data(h5py_file, 'val', x_val_paths, y_val_paths) time = (cv2.getTickCount()-start)/cv2.getTickFrequency() print ('parsing val data, Time:%.3fs'%time) start = cv2.getTickCount() write_data(h5py_file, 'test', x_test_paths, y_test_paths) time = (cv2.getTickCount()-start)/cv2.getTickFrequency() print ('parsing test data, Time:%.3fs'%time)
🌐
OpenCV
docs.opencv.org › 3.4 › dc › d71 › tutorial_py_optimization.html
OpenCV: Performance Measurement and Improvement Techniques
The cv.getTickCount function returns the number of clock-cycles after a reference event (like the moment the machine was switched ON) to the moment this function is called.
🌐
Medium
medium.com › @gracealivingston › cv2-core-operations-performance-measurement-and-im-b6b1670e287b
Study Notes: CV2 — Core Operations — Performance Measurement and Improvement Techniques | by Grace Livingston | Medium
October 31, 2018 - cv2.getTickCount function returns the number of clock-cycles after a reference event (like the moment machine was switched ON) to the moment this function is called.
🌐
Hakancelik
hakancelik.dev › en › performans-olcum-ve-gelistirme-teknikleri
Performance Measurement and Improvement Techniques — Hakan Çelik
March 30, 2022 - >>> import cv2 >>> e1 = cv2.getTickCount() >>> e2 = cv2.getTickCount() >>> e1 311581540258 >>> e2 311594815111
🌐
Sourcecodequery
sourcecodequery.com › example-method › cv2.GetTickCount
SourceCodeQuery - Python cv2.GetTickCount Examples
def run_tracker(tracker, img, gt, video_name, restart=True): frame_counter = 0 lost_number = 0 toc = 0 pred_bboxes = [] if restart: # VOT2016 and VOT 2018 for idx, (img, gt_bbox) in enumerate(video): if len(gt_bbox) == 4: gt_bbox = [gt_bbox[0], gt_bbox[1], gt_bbox[0], gt_bbox[1]+gt_bbox[3]-1, gt_bbox[0]+gt_bbox[2]-1, gt_bbox[1]+gt_bbox[3]-1, gt_bbox[0]+gt_bbox[2]-1, gt_bbox[1]] tic = cv2.getTickCount() if idx == frame_counter: cx, cy, w, h = get_axis_aligned_bbox(np.array(gt_bbox)) gt_bbox_ = [cx-(w-1)/2, cy-(h-1)/2, w, h] tracker.init(img, gt_bbox_) pred_bbox = gt_bbox_ pred_bboxes.append(1)
Find elsewhere
🌐
OpenCV
docs.opencv.org › 2.4 › modules › core › doc › utility_and_system_functions_and_macros.html
Utility and System Functions and Macros — OpenCV 2.4.13.7 documentation
December 31, 2019 - C++: int64 getTickCount()¶ · Python: cv2.getTickCount() → retval¶ · The function returns the number of ticks after the certain event (for example, when the machine was turned on). It can be used to initialize RNG() or to measure a function execution time by reading the tick count before ...
🌐
GitHub
github.com › makelove › OpenCV-Python-Tutorial › blob › master › ch11-程序性能检测及优化 › 11.getTickCount.py
OpenCV-Python-Tutorial/ch11-程序性能检测及优化/11.getTickCount.py at master · makelove/OpenCV-Python-Tutorial
e1 = cv2.getTickCount() · for i in range(5, 49, 2): img1 = cv2.medianBlur(img1, i) · e2 = cv2.getTickCount() t = (e2 - e1) / cv2.getTickFrequency() # 时钟频率 或者 每秒钟的时钟数 · print(t) # 0.034773332 ·
Author: makelove
🌐
ProgramCreek
programcreek.com › python › example › 89429 › cv2.getTickFrequency
Python Examples of cv2.getTickFrequency
def elapsed(self): if self._is_paused: self._elapsed = self._accumulated else: now = cv2.getTickCount() self._elapsed = self._accumulated + (now - self._start_time)/cv2.getTickFrequency() if self._is_verbose is True: name = self._name if self._is_paused: name += ' [paused]' message = 'Timer::' + name + ' - elapsed: ' + str(self._elapsed) timer_print(message) return self._elapsed
🌐
OpenCV
docs.opencv.org › 3.0-beta › modules › core › doc › utility_and_system_functions_and_macros.html
Utility and System Functions and Macros — OpenCV 3.0.0-dev documentation
November 10, 2014 - C++: int64 getTickCount()¶ · Python: cv2.getTickCount() → retval¶ · The function returns the number of ticks after the certain event (for example, when the machine was turned on). It can be used to initialize RNG() or to measure a function execution time by reading the tick count before ...
🌐
OpenCV
docs.opencv.org › 4.13.0 › db › de0 › group__core__utils.html
OpenCV: Utility and system functions and macros
The function returns the current number of CPU ticks on some architectures (such as x86, x64, PowerPC). On other platforms the function is equivalent to getTickCount. It can also be used for very accurate time measurements, as well as for RNG initialization. Note that in case of multi-CPU systems ...
🌐
Jancy Standard Library
vovkos.github.io › doxyrest-showcase › opencv › sphinx_rtd_theme › class_cv_TickMeter.html
class cv::TickMeter — OpenCV Documentation
getTickCount, getTickFrequency · int64 getCounter() const · returns internal counter value. double getTimeMicro() const · returns passed time in microseconds. double getTimeMilli() const · returns passed time in milliseconds. double getTimeSec() const · returns passed time in seconds.
🌐
Hexo
loopvoid.github.io › 2017 › 02 › 23 › Opencv计算算法运行时间
Opencv计算算法运行时间 - Hexo
March 18, 2022 - getTickCount():返回CPU自某个时间(如启动电脑)以来走过的时钟周期数。 · getTickFrequency()返回CPU一秒中所走的时钟周期数。所以可以以秒为单位对某运算时间计时。 · Opencv · Opencv 计算时间 · 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处! ·
🌐
OpenCV
docs.opencv.org › 3.4 › db › da5 › tutorial_how_to_scan_images.html
OpenCV: How to scan images, lookup tables and time measurement with OpenCV
Another issue is how do we measure time? Well OpenCV offers two simple functions to achieve this cv::getTickCount() and cv::getTickFrequency() . The first returns the number of ticks of your systems CPU from a certain event (like since you booted your system).
🌐
Tiziran
tiziran.com › topics-and-projects › source-code › opencv
Computer Vision, Deep Learning, Artificial superintelligence (ASI) - OpenCV
cv2.imwrite(filename, array, params=(cv2.IMWRITE_TIFF_COMPRESSION, 32946)) # Deflate · cv::imwrite("far.png", resizedImage, { cv::IMWRITE_PNG_COMPRESSION, 0 }); Save images in streaming · int64 t0 = cv::getTickCount(); std::string fileName= "fashid_"+std::to_string(t0)+ ".png"; cv::imwrite(fileName, cimMat, { cv::IMWRITE_PNG_COMPRESSION, 0 }); Save file name: std::filesystem::path p = std::filesystem::path(files[i]).filename(); std::string imgFile = savePath + "/" + p.string() + ".tiff"; cv2.imwrite(filename, array, params=(cv2.IMWRITE_TIFF_COMPRESSION, 1)) ; Exception thrown at 0x00007FFB5CB21636 (vcruntime140d.dll) in farshid.exe: 0xC0000005: Access violation writing location 0x000002B09BAC4000.