From d2bad6febb9e8f76ca67e485905c0575d1a311ad Mon Sep 17 00:00:00 2001 From: Suleyman TURKMEN Date: Wed, 25 May 2016 17:16:09 +0300 Subject: [PATCH] cv::TickMeter class addition --- modules/core/include/opencv2/core/utility.hpp | 120 +++++++++++++++++- samples/gpu/cascadeclassifier.cpp | 2 - samples/gpu/generalized_hough.cpp | 2 - samples/gpu/stereo_multi.cpp | 2 - samples/gpu/super_resolution.cpp | 2 - samples/gpu/tick_meter.hpp | 48 ------- samples/gpu/video_reader.cpp | 2 - samples/gpu/video_writer.cpp | 2 - 8 files changed, 119 insertions(+), 61 deletions(-) delete mode 100644 samples/gpu/tick_meter.hpp diff --git a/modules/core/include/opencv2/core/utility.hpp b/modules/core/include/opencv2/core/utility.hpp index 60b2d3a064..c74c1184f2 100644 --- a/modules/core/include/opencv2/core/utility.hpp +++ b/modules/core/include/opencv2/core/utility.hpp @@ -251,7 +251,8 @@ CV_EXPORTS_W const String& getBuildInformation(); 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 and after the function call. See also the tick frequency. +tick count before and after the function call. +@sa getTickFrequency, TickMeter */ CV_EXPORTS_W int64 getTickCount(); @@ -264,9 +265,126 @@ execution time in seconds: // do something ... t = ((double)getTickCount() - t)/getTickFrequency(); @endcode +@sa getTickCount, TickMeter */ CV_EXPORTS_W double getTickFrequency(); +/** @brief a Class to measure passing time. + +The class computes passing time by counting the number of ticks per second. That is, the following code computes the +execution time in seconds: +@code +TickMeter tm; +tm.start(); +// do something ... +tm.stop(); +std::cout << tm.getTimeSec(); +@endcode +@sa getTickCount, getTickFrequency +*/ + +class CV_EXPORTS_W TickMeter +{ +public: + //! the default constructor + CV_WRAP TickMeter() + { + reset(); + } + + /** + starts counting ticks. + */ + CV_WRAP void start() + { + startTime = cv::getTickCount(); + } + + /** + stops counting ticks. + */ + CV_WRAP void stop() + { + int64 time = cv::getTickCount(); + if (startTime == 0) + return; + ++counter; + sumTime += (time - startTime); + startTime = 0; + } + + /** + returns counted ticks. + */ + CV_WRAP int64 getTimeTicks() const + { + return sumTime; + } + + /** + returns passed time in microseconds. + */ + CV_WRAP double getTimeMicro() const + { + return getTimeMilli()*1e3; + } + + /** + returns passed time in milliseconds. + */ + CV_WRAP double getTimeMilli() const + { + return getTimeSec()*1e3; + } + + /** + returns passed time in seconds. + */ + CV_WRAP double getTimeSec() const + { + return (double)getTimeTicks() / getTickFrequency(); + } + + /** + returns internal counter value. + */ + CV_WRAP int64 getCounter() const + { + return counter; + } + + /** + resets internal values. + */ + CV_WRAP void reset() + { + startTime = 0; + sumTime = 0; + counter = 0; + } + +private: + int64 counter; + int64 sumTime; + int64 startTime; +}; + +/** @brief output operator +@code +TickMeter tm; +tm.start(); +// do something ... +tm.stop(); +std::cout << tm; +@endcode +*/ + +static inline +std::ostream& operator << (std::ostream& out, const TickMeter& tm) +{ + return out << tm.getTimeSec() << "sec"; +} + /** @brief Returns the number of CPU ticks. The function returns the current number of CPU ticks on some architectures (such as x86, x64, diff --git a/samples/gpu/cascadeclassifier.cpp b/samples/gpu/cascadeclassifier.cpp index f6209f9fa3..156efbf035 100644 --- a/samples/gpu/cascadeclassifier.cpp +++ b/samples/gpu/cascadeclassifier.cpp @@ -13,8 +13,6 @@ #include "opencv2/cudaimgproc.hpp" #include "opencv2/cudawarping.hpp" -#include "tick_meter.hpp" - using namespace std; using namespace cv; using namespace cv::cuda; diff --git a/samples/gpu/generalized_hough.cpp b/samples/gpu/generalized_hough.cpp index fb1cb8979c..7b7e80ab9d 100644 --- a/samples/gpu/generalized_hough.cpp +++ b/samples/gpu/generalized_hough.cpp @@ -8,8 +8,6 @@ #include "opencv2/cudaimgproc.hpp" #include "opencv2/highgui.hpp" -#include "tick_meter.hpp" - using namespace std; using namespace cv; diff --git a/samples/gpu/stereo_multi.cpp b/samples/gpu/stereo_multi.cpp index bfb3e8a48b..7ef656719d 100644 --- a/samples/gpu/stereo_multi.cpp +++ b/samples/gpu/stereo_multi.cpp @@ -17,8 +17,6 @@ #include "opencv2/imgproc.hpp" #include "opencv2/cudastereo.hpp" -#include "tick_meter.hpp" - using namespace std; using namespace cv; using namespace cv::cuda; diff --git a/samples/gpu/super_resolution.cpp b/samples/gpu/super_resolution.cpp index 026afd9710..94a922cdba 100644 --- a/samples/gpu/super_resolution.cpp +++ b/samples/gpu/super_resolution.cpp @@ -11,8 +11,6 @@ #include "opencv2/superres/optical_flow.hpp" #include "opencv2/opencv_modules.hpp" -#include "tick_meter.hpp" - using namespace std; using namespace cv; using namespace cv::superres; diff --git a/samples/gpu/tick_meter.hpp b/samples/gpu/tick_meter.hpp deleted file mode 100644 index c11a22d140..0000000000 --- a/samples/gpu/tick_meter.hpp +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef OPENCV_CUDA_SAMPLES_TICKMETER_ -#define OPENCV_CUDA_SAMPLES_TICKMETER_ - -class CV_EXPORTS TickMeter -{ -public: - TickMeter(); - void start(); - void stop(); - - int64 getTimeTicks() const; - double getTimeMicro() const; - double getTimeMilli() const; - double getTimeSec() const; - int64 getCounter() const; - - void reset(); -private: - int64 counter; - int64 sumTime; - int64 startTime; -}; - -std::ostream& operator << (std::ostream& out, const TickMeter& tm); - - -TickMeter::TickMeter() { reset(); } -int64 TickMeter::getTimeTicks() const { return sumTime; } -double TickMeter::getTimeMicro() const { return getTimeMilli()*1e3;} -double TickMeter::getTimeMilli() const { return getTimeSec()*1e3; } -double TickMeter::getTimeSec() const { return (double)getTimeTicks()/cv::getTickFrequency();} -int64 TickMeter::getCounter() const { return counter; } -void TickMeter::reset() {startTime = 0; sumTime = 0; counter = 0; } - -void TickMeter::start(){ startTime = cv::getTickCount(); } -void TickMeter::stop() -{ - int64 time = cv::getTickCount(); - if ( startTime == 0 ) - return; - ++counter; - sumTime += ( time - startTime ); - startTime = 0; -} - -std::ostream& operator << (std::ostream& out, const TickMeter& tm) { return out << tm.getTimeSec() << "sec"; } - -#endif diff --git a/samples/gpu/video_reader.cpp b/samples/gpu/video_reader.cpp index d8d6e136f8..a40a6800b4 100644 --- a/samples/gpu/video_reader.cpp +++ b/samples/gpu/video_reader.cpp @@ -14,8 +14,6 @@ #include #include -#include "tick_meter.hpp" - int main(int argc, const char* argv[]) { if (argc != 2) diff --git a/samples/gpu/video_writer.cpp b/samples/gpu/video_writer.cpp index 6c5d1412d6..80d2cfc47b 100644 --- a/samples/gpu/video_writer.cpp +++ b/samples/gpu/video_writer.cpp @@ -11,8 +11,6 @@ #include "opencv2/cudacodec.hpp" #include "opencv2/highgui.hpp" -#include "tick_meter.hpp" - int main(int argc, const char* argv[]) { if (argc != 2)