Merge pull request #17071 from mshabunin:tickmeter-fps

This commit is contained in:
Alexander Alekhin 2020-04-17 08:56:29 +00:00
commit e92f1eaa3d
2 changed files with 78 additions and 59 deletions

View File

@ -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:

View File

@ -2,6 +2,7 @@
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/features2d.hpp>
#include <iostream>
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;
}