C + + clock () parsing how to use clock timings application _c language

Source: Internet
Author: User
The timing function in C/s + + is clock (), and its associated data type is clock_t.
In MSDN, the clock function is identified as follows: clock_t clock (void); This function returns the number of CPU clock timing units (clock tick) between the time the "start this program process" and the call to clock () function in the program), which is called the Wall Clock Time (Wal-clock) in MSDN. Where clock_t is the data type used to hold the time, in the Time.h file, We can find the definition of it:
#ifndef _clock_t_defined
typedef long CLOCK_T;
#define _clock_t_defined
#endif
Obviously, clock_t is a long shaping number. In the time.h file, a constant clocks_per_sec is also defined to indicate how many clock ticks will be in a second, defined as follows:
#define CLOCKS_PER_SEC ((clock_t) 1000) can see every 1 per thousand seconds (1 milliseconds), and the value returned by the clock () function is added 1.
Copy Code code as follows:

#include <iostream>
#include <ctime>
using namespace Std
Int main ()
{
    long n=0;
    clock_t start,finish;
    Start=clock ();
    while (n<1000000000)
        n++;
    Finish=clock ();
    cout<< (finish-start)/clocks_per_sec<<endl;
    return 0;
}
Related Article

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.