Skip to content

cv::TickMeter class addition#6584

Closed
sturkmen72 wants to merge 1 commit intoopencv:masterfrom
sturkmen72:TickMeter_class_addition
Closed

cv::TickMeter class addition#6584
sturkmen72 wants to merge 1 commit intoopencv:masterfrom
sturkmen72:TickMeter_class_addition

Conversation

@sturkmen72
Copy link
Copy Markdown
Contributor

resolves #6582

What does this PR change?

moves cv::TickMeter class in the core library

initial commit

@paroj
Copy link
Copy Markdown
Contributor

paroj commented May 25, 2016

never really missed that class..

But I think it could be more useful if it allowed computing the average time over multiple runs instead of just

auto start = cv::getTickCount();
...
(cv::getTickCount() - start)/cv::getTickFrequency();

@sturkmen72
Copy link
Copy Markdown
Contributor Author

@paroj actually it allows to calculate the average time over multiple runs


#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    TickMeter tm;
    Mat test(1000, 1000, CV_8U);
    randu(test, 0, 255);
    Mat thresh;
    cout << "thresholding a 1000x1000 Mat 5000 times\n\n";
    for (int i = 0; i < 5000; i++)
    {
        tm.start();
        threshold(test, thresh, 230, 255, THRESH_BINARY);
        tm.stop();
    }

    cout << "Total Time in microseconds :" << tm.getTimeMicro() << endl;
    cout << "Average Time in microseconds :" << tm.getTimeMicro() / tm.getCounter() << endl;

    cout << "Total Time in milliseconds :" << tm.getTimeMilli() << endl;
    cout << "Average Time in milliseconds :" << tm.getTimeMilli() / tm.getCounter() << endl;

    cout << "Total Time in seconds :" << tm.getTimeSec() << endl;
    cout << "Average Time in seconds :" << tm.getTimeSec() / tm.getCounter() << endl;

    imshow("thresh", thresh);
    waitKey();
    return 0;
}

output:

thresholding a 1000x1000 Mat 5000 times

Total Time in microseconds :317603
Average Time in microseconds :63.5205
Total Time in milliseconds :317.603
Average Time in milliseconds :0.0635205
Total Time in seconds :0.317603
Average Time in seconds :6.35205e-05

@StevenPuttemans
Copy link
Copy Markdown

Hello @sturkmen72 in relation to issue #6502, do you know if the tickMeter reports the same time in both windows and linux systems? I am looking for a universal way of providing time, even if multicore processing is being used.

@sturkmen72
Copy link
Copy Markdown
Contributor Author

@StevenPuttemans Sorry i have no idea about linux systems :(

class CV_EXPORTS_W TickMeter
{
public:
//! the default constructor
Copy link
Copy Markdown
Contributor Author

@sturkmen72 sturkmen72 May 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any help about explanation of methods is appreciated.
what i added can be seen http://pullrequest.opencv.org/buildbot/export/pr/6584/docs/d9/d6f/classcv_1_1TickMeter.html. my english is not good. so additions and corrections are welcome.

@FredFG
Copy link
Copy Markdown

FredFG commented May 26, 2016

Time is tricky. It is so critical to modern computing there's an ISO standard: iso.20.11.7
C++ implements this standard.
TickMeter is in OpenCv version 2.4 and therefore is C based. I wouldn't use it.
It should be deprecated.

Do your own timing with C++ methods in std::chrono and you should get comparable results on any modern OS or computer.

A simple example from Stroustrup The C++ Programming Language, 4th edition. Section 35.2.

steady_clock_time::time_point t = steady_clock::now();
// do your stuff
steady_clock::duration d1 = steady_clock::now() - t;
// do some more stuff
steady_clock::duration d2 = steady_clock::now() - d1;

There's nothing easier than that.
You can get resolutions down to picoseconds (depending on your hardware). It doesn't care how many cpus are running or what you had for breakfast.

Really precise timing might get weird on a "really cheap" CPU such as Raspberry Pi or Arduino. Even these errors would be so small they would likely be meaningless unless you're an astrophysicist.

@StevenPuttemans
Copy link
Copy Markdown

I kind of agree with @FredFG that the absence of ISO standards for time and the introduction of them using C++11 can help us to make a more universal approach. However, C++11 is far from the standard compiler for now, so maybe we should add the 2 cases

  1. if c++11 is available use new functionality
  2. else fall back

startTime = 0;
}

std::ostream& operator << (std::ostream& out, const TickMeter& tm) { return out << tm.getTimeSec() << "sec"; }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation is very light, so I suggest to put all methods it into header directly (if bindings generator is fine to bypass this code).
Especially for ostream-based operator <<

@sturkmen72 sturkmen72 force-pushed the TickMeter_class_addition branch 2 times, most recently from dd1ca6f to 34ab5c3 Compare May 27, 2016 19:33

#endif

std::ostream& operator << (std::ostream& out, const TickMeter& tm)
Copy link
Copy Markdown
Contributor Author

@sturkmen72 sturkmen72 May 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alalek i tried to move this part in utility.hpp but get errors. what do you suggest?

indeed i see this part is trivial. it can be removed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What kind of errors do you have? Did you try "static inline" without CV_EXPORTS?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alalek thanks. it is OK with "static inline"

@sturkmen72 sturkmen72 force-pushed the TickMeter_class_addition branch from 6898513 to a38830e Compare June 23, 2016 15:33
@sturkmen72 sturkmen72 force-pushed the TickMeter_class_addition branch from a38830e to d2bad6f Compare June 23, 2016 16:13
@vpisarev
Copy link
Copy Markdown
Contributor

vpisarev commented Jul 8, 2016

this PR will be recreated because of CI issues

@vpisarev vpisarev closed this Jul 8, 2016
@sturkmen72 sturkmen72 deleted the TickMeter_class_addition branch July 14, 2016 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request cv::TickMeter

7 participants