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/>