C ++ program running time

Source: Internet
Author: User

In C ++ProgramIn the performance analysis, one of the most important items is the running time of the program. Although the program running speed is closely related to the computer configuration and the current state of the computer, in a relatively consistent external environment, the length of the program running time can reflect the program efficiency to a large extent.

1. General Timing Method

In the ctime header file, C ++ provides the timing function clock (). The returned data type is clock_t.

 
TypedefLongClock_t;

The clock () function returns the number of clock units (clock tick) of the CPU during the period from "starting the program process" to "calling the clock () function in the program, in msdn, it is called the wall clock time (Wal-clock ).
In the ctime file, a constant clocks_per_sec is also defined to indicate the number of CPU clock units in one second.
You can use clock ()/clocks_per_sec to get the running time of the process. Generally, the value of clocks_per_sec is 1000. It can be seen that the timing precision can reach three decimal places (in milliseconds ).

 
# DefineClk_tck clocks_per_sec

A simple example of timing is as follows:

 /*  * Author: Hou Kai * Description: Clock () Timing function * Date:  */ # Include <Ctime> //  Timing header file # Include <iostream> Using   Namespace  STD;  Int  Main (){  Long I = 0000000l  ; Clock_t start, end; Start = Clock ();  While (I --); // Program segment for timing End = Clock (); printf (  "  The time was: % F \ n  " ,( Double ) (End-Start )/ Clk_tck); System (  "  Pause  "  );  Return   0  ;} 

2. Exact timing method

The exact meaning here is that the timing accuracy is higher. In order to achieve a higher timing accuracy, the precise time functions queryperformancecounter () and queryperformancefrequency () need to be used. They require computers to support accurate timers on hardware. Of course, computers are generally supported now.
The queryperformancecounter () function returns the number of pulses (times) of the high-precision counter. The queryperformancefrequency () function provides the frequency value of the high-precision timer, that is, the number of pulses per second.

 
BoolQueryperformancefrequency (large_integer *Lpfrequency );BoolQueryperformancecounter (large_integer * lpcount );

The data type large_integer can be an 8-byte long integer or a union structure of two 4-byte long integer numbers, the specific usage depends on whether the compiler supports 64-bit. This type is defined as follows:

Typedef union _ large_integer {Struct{DWORD lowpart;//4-byte integerLong highpart;//4-byte integer}; Longlong quadpart;//8-byte integer} Large_integer;

Before timing, call the queryperformancefrequency () function to obtain the clock frequency of the machine's Internal timer, and then call the queryperformancecounter () function before and after a strictly scheduled event, the precise time of event experience is calculated based on the difference between the two counts and the clock frequency. The process is similar to the clock () method, but the clock frequency here is very high. The quadpart value of the test computer frequency is 3118031, and its accuracy is higher than that of clock ().
To better use this timing method, it has been encapsulated into the hptime class,Download. The timer program is as follows:

 /* * Author: Hou Kai * Note: hptime high-precision timing * Date:  */  # Include  "  Hptime. h  "  # Include <Iostream> Using   Namespace  STD;  Int  Main (){  Long I = 0000000l  ; Hptime; While (I --); //  Function segment to be timed  Printf (  "  The time was: % F \ n  "  , Hptime. Sec (); System (  "  Pause  "  );  Return   0  ;} 

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.