Detailed introduction to the use of C/C ++ time functions (1)

Source: Internet
Author: User

The following describes the time functions in C/C ++ for your reference.

1. Definition of time

C/C ++ has many time-related operations worth your attention. Recently, many netizens in the Technical Group have asked questions about time operations, acquisition and display in C ++. Next, in this article, I will mainly introduce how to use the time and date in C/C ++.

By learning a lot of C/C ++ libraries, you can have a lot of operations and time-consuming methods. But before that, you need to understand the concepts of "time" and "date", mainly including the following:

Coordinated Universal TimeUTC): coordinates the Universal Time, also known as the world standard Time, that is, the well-known Greenwich Mean Time, GMT ). For example, the time difference between mainland China and UTC is + 8, that is, UTC + 8. The United States is UTC-5.

Calendar Time: indicates the Calendar Time, which is expressed by the number of seconds elapsed from a standard Time point to the current Time. This standard time point varies with compilers, but for a compilation system, this standard time point remains unchanged, the calendar time corresponding to the time in the compilation system is measured by the standard time point. Therefore, it can be said that the calendar time is relative time, But no matter which time zone you are in, the calendar time is the same for the same standard time point at the same time point.

Epoch: time point. The time point is an integer in the Standard C/C ++, which is expressed by the number of seconds that are different from the standard time point, that is, the calendar time.

Clock tick: A clock timing unit, instead of a clock tick.) The duration of a clock timing unit is controlled by the CPU. A clock tick is not a clock cycle of the CPU, but a basic unit of time for C/C ++.

We can use the time. h header file in the ANSI standard library. The method used to define the time and date in this header file, whether in the structure definition or naming, has a clear C language style. Next, I will explain how to use the date function in C/C ++.

2. Timing

In C/C ++, the timing function is clock, and the related data type is clock_t. In MSDN, the clock function is defined as follows:

 
 
  1. clock_t clock void ); 

This function returns the number of CPU clock timer units (clock tick) between "enable this program process" and "program call clock) function, 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:

 
 
  1. #define CLOCKS_PER_SEC clock_t)1000) 

We can see that every 1‰ seconds, 1 millisecond), call clock) the value returned by the function is 1. for example, you can use clock)/CLOCKS_PER_SEC to calculate the running time of a process:

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

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 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 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.


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.