From 679931dcdeb72d29b0f683a81d3f69dd41b0d943 Mon Sep 17 00:00:00 2001 From: Javier Macias Sola Date: Sat, 28 Sep 2024 12:24:36 +0200 Subject: [PATCH] Enhance cv::TickMeter to be able to get the last ellapsed time --- modules/core/include/opencv2/core/utility.hpp | 37 ++++++++++++++++--- .../tutorial_code/snippets/core_various.cpp | 1 + 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/modules/core/include/opencv2/core/utility.hpp b/modules/core/include/opencv2/core/utility.hpp index 5eed688c83..87a0fe2ff8 100644 --- a/modules/core/include/opencv2/core/utility.hpp +++ b/modules/core/include/opencv2/core/utility.hpp @@ -309,11 +309,12 @@ public: //! stops counting ticks. CV_WRAP void stop() { - int64 time = cv::getTickCount(); + const int64 time = cv::getTickCount(); if (startTime == 0) return; ++counter; - sumTime += (time - startTime); + lastTime = time - startTime; + sumTime += lastTime; startTime = 0; } @@ -336,11 +337,35 @@ public: } //! returns passed time in seconds. - CV_WRAP double getTimeSec() const + CV_WRAP double getTimeSec() const { return (double)getTimeTicks() / getTickFrequency(); } + //! returns counted ticks of the last iteration. + CV_WRAP int64 getLastTimeTicks() const + { + return lastTime; + } + + //! returns passed time of the last iteration in microseconds. + CV_WRAP double getLastTimeMicro() const + { + return getLastTimeMilli()*1e3; + } + + //! returns passed time of the last iteration in milliseconds. + CV_WRAP double getLastTimeMilli() const + { + return getLastTimeSec()*1e3; + } + + //! returns passed time of the last iteration in seconds. + CV_WRAP double getLastTimeSec() const + { + return (double)getLastTimeTicks() / getTickFrequency(); + } + //! returns internal counter value. CV_WRAP int64 getCounter() const { @@ -373,15 +398,17 @@ public: //! resets internal values. CV_WRAP void reset() { - startTime = 0; - sumTime = 0; counter = 0; + sumTime = 0; + startTime = 0; + lastTime = 0; } private: int64 counter; int64 sumTime; int64 startTime; + int64 lastTime; }; /** @brief output operator diff --git a/samples/cpp/tutorial_code/snippets/core_various.cpp b/samples/cpp/tutorial_code/snippets/core_various.cpp index 2be97f989d..b3d590100d 100644 --- a/samples/cpp/tutorial_code/snippets/core_various.cpp +++ b/samples/cpp/tutorial_code/snippets/core_various.cpp @@ -78,6 +78,7 @@ int main() tm.start(); // do something ... tm.stop(); + cout << "Last iteration: " << tm.getLastTimeSec() << endl; } cout << "Average time per iteration in seconds: " << tm.getAvgTimeSec() << endl; cout << "Average FPS: " << tm.getFPS() << endl;