C ++ program timing method

Source: Internet
Author: User

The following uses an example to briefly describe the timing method of the C ++ program.

# Include <iostream>
# Include <ctime>
Using namespace STD;
Void main ()
{
Clock_t start, finish;
Start = clock ();
Int one;
Int & Rone = one;
One = 5;
Cout <"One:" <one <Endl;
Cout <"Rone:" <Rone <Endl;
Cout <"& one:" <& one <Endl;
Cout <"& Rone:" <& Rone <Endl;
Int two = 28;
Rone = two;
Cout <"One:" <one <Endl;
Cout <"two:" <two <Endl;
Cout <"Rone:" <Rone <Endl;
Cout <"& one:" <& one <Endl;
Cout <"& two:" <& Two <Endl;
Cout <"& Rone:" <& Rone <Endl;
Finish = clock ();
Cout <"time spent" <(finish-Start)/clocks_per_sec <"S" <Endl;
}

 

§ Program timing requires you to know the time when the program starts computing and ends computing. c ++ program timing can directly reference the CPU pulse-Pulse Count Special Data Type: clock_t-Number of pulses per second: clocks_per_sec is actually a macro # define clocks_per_sec 1000-header file: <ctime>

Define the Pulse Count variable:

-

Start = clock ();{...} Finish = clock (); cout <"The operation time consumed is:" <double (finish-Start)/clocks_per_sec

Clock_t start, finish;

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.