OpenCV deep learning (3)-opencv function for measuring running time

Source: Internet
Author: User

In the initial C interface, cvGetTickCount () and cvGetTickFrequency () are used. cvGetTickCount () is used twice at the beginning and end of the program segment, and the difference is divided by cvGetTickFrequency () then we can get the running time in microsecond us of the program segment, which is not very accurate but generally enough.

2. after x, several more functions are added to the namespace cv, including getTickCount (), getTickFrequency (), and getCPUTickCount (). At this time, you can still use the functions of the above C interface, you can also use the functions in these namespaces cv to use getTickCount () twice and then divide it by getTickFrequency (), however, it should be noted that the calculated time is in seconds. This is different from the previous function with the cv prefix, and more accurate timing is required, getCPUTickCount () is required. However, the CPU frequency of modern computers changes with the load, so it is not necessary to use this function. For more information, see [Also,
Since a modern CPU varies the CPU frequency depending on the load, the number of CPU clocks spent in some code cannot be directly converted to time units. Therefore,GetTickCountIs generally a preferable
Solution for measuring execution time.] That is, using getTickCount is enough.

At the same time, the C ++ interface also encapsulates the above function as a class TickMeter for ease of use. The following is the TickMeter declaration and implementation source code:

//contrib.hpp  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;};CV_EXPORTS std::ostream& operator<<(std::ostream& out, const TickMeter& tm);//Spinimages.cppcv::TickMeter::TickMeter() { reset(); }int64 cv::TickMeter::getTimeTicks() const { return sumTime; }double cv::TickMeter::getTimeMicro() const { return (double)getTimeTicks()/cvGetTickFrequency(); }double cv::TickMeter::getTimeMilli() const { return getTimeMicro()*1e-3; }double cv::TickMeter::getTimeSec()   const { return getTimeMilli()*1e-3; }    int64 cv::TickMeter::getCounter() const { return counter; }void  cv::TickMeter::reset() {startTime = 0; sumTime = 0; counter = 0; }void cv::TickMeter::start(){ startTime = cvGetTickCount(); }void cv::TickMeter::stop(){int64 time = cvGetTickCount();if ( startTime == 0 )return;++counter;sumTime += ( time - startTime );startTime = 0;}std::ostream& cv::operator<<(std::ostream& out, const TickMeter& tm){ return out << tm.getTimeSec() << "sec"; }

 

It can be seen that TickMeter only encapsulates the functions of the C interface, but it is much more convenient to use. You do not need to repeatedly gettickcount and divide it by frequency, you only need to call start () at the beginning; call stop () at the end, and then call getTimeMicro () if you need to run the time in the unit of us (), getTimeMilli () is called in ms. getTimeSec () is measured in seconds, while getTimeTicks () returns the tickcount and getTimeTicks () of the running time () the returned value is the number of calls to stop, and the class also reloads ostream, which can be conveniently output in seconds.

A small example:

TickMeter tm;tm.start();int col=temp.cols, row = temp.rows;if (temp.isContinuous()){col*=row;row =1;}for (i=0; i<row; ++i){const float *pt = temp.ptr<float>(i);for (j=0; j<col;++j){float mm=pt[3*j];mm = mm/(float)20.6;}}tm.stop();cout<<"count="<<tm.getCounter()<<",process time="<<tm.getTimeMilli()<<endl;

[NOTE! The timer of the TickMeter object is cumulative. If you want to perform the multipart timer, call reset () to return the timer to zero before the next start () call, otherwise, the obtained time is the cumulative running time]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.