Skip to content

Commit 1909ac8

Browse files
authored
Merge pull request opencv#26212 from jamacias:feature/TickMeter-lasttime
Enhance cv::TickMeter to be able to get the last elapsed time
2 parents 08f7f13 + 679931d commit 1909ac8

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

modules/core/include/opencv2/core/utility.hpp

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,12 @@ class CV_EXPORTS_W TickMeter
340340
//! stops counting ticks.
341341
CV_WRAP void stop()
342342
{
343-
int64 time = cv::getTickCount();
343+
const int64 time = cv::getTickCount();
344344
if (startTime == 0)
345345
return;
346346
++counter;
347-
sumTime += (time - startTime);
347+
lastTime = time - startTime;
348+
sumTime += lastTime;
348349
startTime = 0;
349350
}
350351

@@ -367,11 +368,35 @@ class CV_EXPORTS_W TickMeter
367368
}
368369

369370
//! returns passed time in seconds.
370-
CV_WRAP double getTimeSec() const
371+
CV_WRAP double getTimeSec() const
371372
{
372373
return (double)getTimeTicks() / getTickFrequency();
373374
}
374375

376+
//! returns counted ticks of the last iteration.
377+
CV_WRAP int64 getLastTimeTicks() const
378+
{
379+
return lastTime;
380+
}
381+
382+
//! returns passed time of the last iteration in microseconds.
383+
CV_WRAP double getLastTimeMicro() const
384+
{
385+
return getLastTimeMilli()*1e3;
386+
}
387+
388+
//! returns passed time of the last iteration in milliseconds.
389+
CV_WRAP double getLastTimeMilli() const
390+
{
391+
return getLastTimeSec()*1e3;
392+
}
393+
394+
//! returns passed time of the last iteration in seconds.
395+
CV_WRAP double getLastTimeSec() const
396+
{
397+
return (double)getLastTimeTicks() / getTickFrequency();
398+
}
399+
375400
//! returns internal counter value.
376401
CV_WRAP int64 getCounter() const
377402
{
@@ -404,15 +429,17 @@ class CV_EXPORTS_W TickMeter
404429
//! resets internal values.
405430
CV_WRAP void reset()
406431
{
407-
startTime = 0;
408-
sumTime = 0;
409432
counter = 0;
433+
sumTime = 0;
434+
startTime = 0;
435+
lastTime = 0;
410436
}
411437

412438
private:
413439
int64 counter;
414440
int64 sumTime;
415441
int64 startTime;
442+
int64 lastTime;
416443
};
417444

418445
/** @brief output operator

samples/cpp/tutorial_code/snippets/core_various.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ int main()
7878
tm.start();
7979
// do something ...
8080
tm.stop();
81+
cout << "Last iteration: " << tm.getLastTimeSec() << endl;
8182
}
8283
cout << "Average time per iteration in seconds: " << tm.getAvgTimeSec() << endl;
8384
cout << "Average FPS: " << tm.getFPS() << endl;

0 commit comments

Comments
 (0)