diff --git a/modules/core/include/opencv2/core/utility.hpp b/modules/core/include/opencv2/core/utility.hpp index a110d34684..b97458d2d3 100644 --- a/modules/core/include/opencv2/core/utility.hpp +++ b/modules/core/include/opencv2/core/utility.hpp @@ -282,107 +282,98 @@ CV_EXPORTS_W double getTickFrequency(); 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 +@snippet snippets/core_various.cpp TickMeter_total It is also possible to compute the average time over multiple runs: -@code -TickMeter tm; -for (int i = 0; i < 100; i++) -{ - tm.start(); - // do something ... - tm.stop(); -} -double average_time = tm.getTimeSec() / tm.getCounter(); -std::cout << "Average time in second per iteration is: " << average_time << std::endl; -@endcode +@snippet snippets/core_various.cpp TickMeter_average + @sa getTickCount, getTickFrequency */ - class CV_EXPORTS_W TickMeter { public: //! the default constructor CV_WRAP TickMeter() { - reset(); + reset(); } - /** - starts counting ticks. - */ + //! starts counting ticks. CV_WRAP void start() { - startTime = cv::getTickCount(); + startTime = cv::getTickCount(); } - /** - stops counting ticks. - */ + //! stops counting ticks. CV_WRAP void stop() { - int64 time = cv::getTickCount(); - if (startTime == 0) - return; - ++counter; - sumTime += (time - startTime); - startTime = 0; + int64 time = cv::getTickCount(); + if (startTime == 0) + return; + ++counter; + sumTime += (time - startTime); + startTime = 0; } - /** - returns counted ticks. - */ + //! returns counted ticks. CV_WRAP int64 getTimeTicks() const { - return sumTime; + return sumTime; } - /** - returns passed time in microseconds. - */ + //! returns passed time in microseconds. CV_WRAP double getTimeMicro() const { - return getTimeMilli()*1e3; + return getTimeMilli()*1e3; } - /** - returns passed time in milliseconds. - */ + //! returns passed time in milliseconds. CV_WRAP double getTimeMilli() const { - return getTimeSec()*1e3; + return getTimeSec()*1e3; } - /** - returns passed time in seconds. - */ + //! returns passed time in seconds. CV_WRAP double getTimeSec() const { - return (double)getTimeTicks() / getTickFrequency(); + return (double)getTimeTicks() / getTickFrequency(); } - /** - returns internal counter value. - */ + //! returns internal counter value. CV_WRAP int64 getCounter() const { - return counter; + return counter; } - /** - resets internal values. - */ + //! returns average FPS (frames per second) value. + CV_WRAP double getFPS() const + { + const double sec = getTimeSec(); + if (sec < DBL_EPSILON) + return 0.; + return counter / sec; + } + + //! returns average time in seconds + CV_WRAP double getAvgTimeSec() const + { + if (counter <= 0) + return 0.; + return getTimeSec() / counter; + } + + //! returns average time in milliseconds + CV_WRAP double getAvgTimeMilli() const + { + return getAvgTimeSec() * 1e3; + } + + //! resets internal values. CV_WRAP void reset() { - startTime = 0; - sumTime = 0; - counter = 0; + startTime = 0; + sumTime = 0; + counter = 0; } private: diff --git a/samples/cpp/tutorial_code/snippets/core_various.cpp b/samples/cpp/tutorial_code/snippets/core_various.cpp index 6e220788ed..4639940856 100644 --- a/samples/cpp/tutorial_code/snippets/core_various.cpp +++ b/samples/cpp/tutorial_code/snippets/core_various.cpp @@ -2,6 +2,7 @@ #include #include #include +#include using namespace std; using namespace cv; @@ -52,5 +53,32 @@ int main() imshow("rectangles", test_image); waitKey(0); //! [RotatedRect_demo] + + { + //! [TickMeter_total] + TickMeter tm; + tm.start(); + // do something ... + tm.stop(); + cout << "Total time: " << tm.getTimeSec() << endl; + //! [TickMeter_total] + } + + { + const int COUNT = 100; + //! [TickMeter_average] + TickMeter tm; + for (int i = 0; i < COUNT; i++) + { + tm.start(); + // do something ... + tm.stop(); + } + cout << "Average time per iteration in seconds: " << tm.getAvgTimeSec() << endl; + cout << "Average FPS: " << tm.getFPS() << endl; + //! [TickMeter_average] + } + + return 0; }