TickMeter: added FPS and AvgTime, improved docs, reformatted

pull/17071/head
Maksim Shabunin 5 years ago
parent f6de25b6cb
commit f84cae833a
  1. 77
      modules/core/include/opencv2/core/utility.hpp
  2. 28
      samples/cpp/tutorial_code/snippets/core_various.cpp

@ -282,29 +282,13 @@ 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 The class computes passing time by counting the number of ticks per second. That is, the following code computes the
execution time in seconds: execution time in seconds:
@code @snippet snippets/core_various.cpp TickMeter_total
TickMeter tm;
tm.start();
// do something ...
tm.stop();
std::cout << tm.getTimeSec();
@endcode
It is also possible to compute the average time over multiple runs: It is also possible to compute the average time over multiple runs:
@code @snippet snippets/core_various.cpp TickMeter_average
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
@sa getTickCount, getTickFrequency @sa getTickCount, getTickFrequency
*/ */
class CV_EXPORTS_W TickMeter class CV_EXPORTS_W TickMeter
{ {
public: public:
@ -314,17 +298,13 @@ public:
reset(); reset();
} }
/** //! starts counting ticks.
starts counting ticks.
*/
CV_WRAP void start() CV_WRAP void start()
{ {
startTime = cv::getTickCount(); startTime = cv::getTickCount();
} }
/** //! stops counting ticks.
stops counting ticks.
*/
CV_WRAP void stop() CV_WRAP void stop()
{ {
int64 time = cv::getTickCount(); int64 time = cv::getTickCount();
@ -335,49 +315,60 @@ public:
startTime = 0; startTime = 0;
} }
/** //! returns counted ticks.
returns counted ticks.
*/
CV_WRAP int64 getTimeTicks() const CV_WRAP int64 getTimeTicks() const
{ {
return sumTime; return sumTime;
} }
/** //! returns passed time in microseconds.
returns passed time in microseconds.
*/
CV_WRAP double getTimeMicro() const 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 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 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 CV_WRAP int64 getCounter() const
{ {
return counter; return counter;
} }
/** //! returns average FPS (frames per second) value.
resets internal values. 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() CV_WRAP void reset()
{ {
startTime = 0; startTime = 0;

@ -2,6 +2,7 @@
#include <opencv2/imgproc.hpp> #include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp> #include <opencv2/highgui.hpp>
#include <opencv2/features2d.hpp> #include <opencv2/features2d.hpp>
#include <iostream>
using namespace std; using namespace std;
using namespace cv; using namespace cv;
@ -52,5 +53,32 @@ int main()
imshow("rectangles", test_image); imshow("rectangles", test_image);
waitKey(0); waitKey(0);
//! [RotatedRect_demo] //! [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; return 0;
} }

Loading…
Cancel
Save