Detailed explanation of C ++ timing operations

Source: Internet
Author: User

The C ++ programming language is widely used and has powerful functions. In the previous article, we have briefly introduced the basic concepts of C ++ time. Now, we will further interpret the specific operations of C ++ timing so that you can gain more knowledge.

  • How to Implement C ++ Polymorphism
  • C ++ static member initialization FAQs
  • Basic concepts of C ++ type conversion
  • C ++ compiler command list Summary
  • C ++ basic concepts of time

The function used in the C ++ timing operation is clock (), and the related data type is clock_t. In MSDN, the clock function is defined as follows:

Clock_t clock (void );

This C ++ timing function returns the number of CPU clock timing units clock tick between "starting this program process" and "calling clock () function in the program, in MSDN, it is called the wall clock time wal-clock ). Clock_t is the data type used to save the time. In the time. h file, we can find its definition:

 
 
  1. #ifndef _CLOCK_T_DEFINED   
  2. typedef long clock_t;   
  3. #define _CLOCK_T_DEFINED   
  4. #endif  

Clock_t is a long integer. In the time. h file, a constant CLOCKS_PER_SEC is also defined to indicate the number of clock units in one second. Its definition is as follows:

# Define CLOCKS_PER_SEC (clock_t) 1000)

You can see that every 1‰ seconds, 1 millisecond), call clock) the value returned by the function is increased by 1. Here is an example of C ++ timing. You can use the formula clock ()/CLOCKS_PER_SEC to calculate the running time of a process itself:

 
 
  1. void elapsed_time()   
  2. {   
  3. printf("Elapsed time:%u secs.\n",clock()/CLOCKS_PER_SEC);   
  4. }  

Of course, you can also use the clock function to calculate how much time your machine spends running a loop or processing other events:

 
 
  1. # Include "stdio. h"
  2. # Include "stdlib. h"
  3. # Include "time. h"
  4. Int main (void)
  5. {
  6. Long I = 0000000l;
  7. Clock_t start, finish;
  8. Double duration;
  9. /* Measure the duration of an event */
  10. Printf ("Time to do % ld empty loops is", I );
  11. Start = clock ();
  12. While (I --);
  13. Finish = clock ();
  14. Duration = (double) (finish-start)/CLOCKS_PER_SEC;
  15. Printf ("% f seconds \ n", duration );
  16. System ("pause ");
  17. }

On the author's machine, the running result after the C ++ timing operation is as follows:

Time to do 10000000 empty loops is 0.03000 seconds

We can see that the length of the clock timing unit is 1 millisecond, and the timing accuracy is also 1 millisecond. Can we change the definition of CLOCKS_PER_SEC and define it larger, so that the timing accuracy of C ++ is higher? By trying, you will find that this is not the case. In standard C/C ++, the minimum unit of time is one millisecond.

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.