C/C ++ timer checks program performance

Source: Internet
Author: User

generally, C/C ++ Program is designed to process data per second, therefore, you can make a simple timer to timing. The Code is as follows:

# Ifndef _ timer_h _ <br/> # DEFINE _ timer_h _ <br/> # include <string> <br/> # include <sys/time. h> <br/> using namespace STD; <br/> class timer {<br/> PRIVATE: <br/> timeval tstart; <br/> timeval tend; <br/> unsigned count; <br/> unsigned print_count; <br/> Public: <br/> timer (): Count (0), print_count (10000) {<br/>}< br/> timer (int pc): Count (0), print_count (PC) {<br/>}< br/> void add () {<br/> count ++; <br/> If (count % print_count = 0) {<br/> end (); <br/> begin (); <br/>}< br/> void begin () {<br/> gettimeofday (& tstart, null ); <br/>}< br/> void end () {<br/> gettimeofday (& tend, null); <br/> double linstart = (double) tstart. TV _sec * 1000000 + (double) tstart. TV _usec); // UNIT S <br/> double linend = (double) tend. TV _sec * 1000000 + (double) tend. TV _usec); // UNIT S <br/> Double Delta = (linend-linstart)/1000000; // second <br/> printf ("Timer: % d % F/N ", print_count, Count, Delta, print_count/delta); <br/>}< br/> }; <br/> # endif/* _ timer_h _*/

 

The call method is as follows:

Timer timer (10000); // how many pieces of data are printed once <br/> timer. begin (); // start timing <br/> for (;) {<br/> timer. add (); // increase to print when the number of prints reaches <br/>}< br/> timer. end (); // print the last time <br/>

 

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.