Skip to content

Commit 679931d

Browse files
committed
Enhance cv::TickMeter to be able to get the last ellapsed time
1 parent 450e741 commit 679931d

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
@@ -309,11 +309,12 @@ class CV_EXPORTS_W TickMeter
309309
//! stops counting ticks.
310310
CV_WRAP void stop()
311311
{
312-
int64 time = cv::getTickCount();
312+
const int64 time = cv::getTickCount();
313313
if (startTime == 0)
314314
return;
315315
++counter;
316-
sumTime += (time - startTime);
316+
lastTime = time - startTime;
317+
sumTime += lastTime;
317318
startTime = 0;
318319
}
319320

@@ -336,11 +337,35 @@ class CV_EXPORTS_W TickMeter
336337
}
337338

338339
//! returns passed time in seconds.
339-
CV_WRAP double getTimeSec() const
340+
CV_WRAP double getTimeSec() const
340341
{
341342
return (double)getTimeTicks() / getTickFrequency();
342343
}
343344

345+
//! returns counted ticks of the last iteration.
346+
CV_WRAP int64 getLastTimeTicks() const
347+
{
348+
return lastTime;
349+
}
350+
351+
//! returns passed time of the last iteration in microseconds.
352+
CV_WRAP double getLastTimeMicro() const
353+
{
354+
return getLastTimeMilli()*1e3;
355+
}
356+
357+
//! returns passed time of the last iteration in milliseconds.
358+
CV_WRAP double getLastTimeMilli() const
359+
{
360+
return getLastTimeSec()*1e3;
361+
}
362+
363+
//! returns passed time of the last iteration in seconds.
364+
CV_WRAP double getLastTimeSec() const
365+
{
366+
return (double)getLastTimeTicks() / getTickFrequency();
367+
}
368+
344369
//! returns internal counter value.
345370
CV_WRAP int64 getCounter() const
346371
{
@@ -373,15 +398,17 @@ class CV_EXPORTS_W TickMeter
373398
//! resets internal values.
374399
CV_WRAP void reset()
375400
{
376-
startTime = 0;
377-
sumTime = 0;
378401
counter = 0;
402+
sumTime = 0;
403+
startTime = 0;
404+
lastTime = 0;
379405
}
380406

381407
private:
382408
int64 counter;
383409
int64 sumTime;
384410
int64 startTime;
411+
int64 lastTime;
385412
};
386413

387414
/** @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)