C ++ Timer

Source: Internet
Author: User

ProgramThe timing function is often usedCodeTherefore, the clock () function is often used, which is included in the header file of time. h.

The timing function is based on the clock_t data type and can be found in the time. h file:

/* Define the implementation defined time type */</P> <p> # ifndef _ time_t_defined <br/> typedef long time_t; /* Time Value */<br/> # DEFINE _ time_t_defined/* avoid multiple Def's of time_t */<br/> # endif </P> <p> # ifndef _ clock_t_defined <br/> typedef long clock_t; <br/> # DEFINE _ clock_t_defined <br/> # endif

Clock_t actually refers to the Data Type of long integer.

 

The specific usage of the timing function is as follows:

# Include <stdio. h> <br/> # include <time. h> </P> <p> int main () <br/>{< br/> clock_t start, finish; <br/> Start = clock (); </P> <p> int counter = 0; <br/> for (INT I = 0; I <1000000; I ++) <br/>{< br/> counter ++; <br/>}</P> <p> finish = clock (); </P> <p> printf ("the program running time is % d seconds/N", (finish-Start)/clocks_per_sec ); <br/>/* Clock ticks macro-ANSI version */<br/> // clocks_per_sec at time. <br/> // # define clocks_per_sec 1000 <br/> // describe clock () the return value is millisecond </P> <p> getchar (); <br/> return 0; <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.