Summary of several methods for obtaining time and calculating time difference in windows c/c ++

Source: Internet
Author: User
Tags sleep function
Document directory
  •  
  •  
Summary of several methods for obtaining time and calculating time difference in windows (from http://blog.csdn.net/coder_xia/article/details/6566708)

I. Standard C and C ++ are available

1. Use time_t to obtain the timeTime(Time_t * timer), calculate the time difference using double difftime (time_t timer1, time_t timer0 ). Accurate to seconds.

The test procedure is as follows:

[C-sharp]View plaincopyprint?
  1. # Include <time. h>
  2. # Include <stdio. h>
  3. Int main ()
  4. {
  5. Time_t start, end;
  6. Double cost;
  7. Time (& start );
  8. Sleep (1 );
  9. Time (& end );
  10. Cost = difftime (end, start );
  11. Printf ("% f/n", cost );
  12. Return 0;
  13. }

This program passed the test in fedora9.

For the sleep function in the code, note the following:

1) In windows, it is a Sleep function and contains windows. h.

2) for sleep, 1000 in Windows and Linux means different meanings. In Windows, 1000 is represented in milliseconds, that is, 1 second. in Linux, 1000 is represented in seconds, in Linux, usleep can be used for millisecond-level functions.

 

2. clock_tClock (), Clock ()

The time interval after the computer is started is obtained, and the CPU time is accurate to 1/CLOCKS_PER_SEC.

The test procedure is as follows:

[C-sharp]View plaincopyprint?
  1. # Include <time. h>
  2. # Include <stdio. h>
  3. Int main ()
  4. {
  5. Double start, end, cost;
  6. Start = clock ();
  7. Sleep (1 );
  8. End = clock ();
  9. Cost = end-start;
  10. Printf ("% f/n", cost );
  11. Return 0;
  12. }

II. C ++ (for windows, both linux and windows are supported in Standard c)

1,GetTickCount ()

The call function must contain windows. h. The system runs in milliseconds. The test procedure is as follows:

[C-sharp]View plaincopyprint?
  1. # Include <iostream>
  2. # Include <windows. h>
  3. Using namespace std;
  4. Int main ()
  5. {
  6. Double start = GetTickCount ();
  7. Sleep (1000 );
  8. Double end = GetTickCount ();
  9. Cout <"GetTickCount:" <end-start <endl;
  10. Return 0;
  11. }

2,GetLocalTime ()

The year, month, and other information stored by the struct are obtained. In C language, the time function is obtained from 00:00:00 on January 1, January 1, 1970 to the number of seconds at this time. The gmtime function is required to convert to a commonly used calendar (the returned result is world time. To display the commonly used time, it is the localtime function ).

In the C language, the structure for storing commonly used calendars is struct tm, which is contained in time. in h, the c ++ language is the SYSTEMTIME struct, which is contained in winbase. h (programming includes windows. h ). Of course, the precision must be seconds.

The test procedure is as follows:

[C-sharp]View plaincopyprint?
  1. # Include <iostream>
  2. # Include <windows. h>
  3. Using namespace std;
  4. Int main ()
  5. {
  6. SYSTEMTIME start; // in windows. h
  7. GetLocalTime (& start); // The tm struct of time. h has the same effect.
  8. Cout <start. year <endl;
  9. }

The example code of the gmtime method in C language is as follows:

[C-sharp]View plaincopyprint?
  1. # Include <time. h>
  2. # Include <stdio. h>
  3. # Include <stdlib. h>
  4. Int main ()
  5. {
  6. Struct tm * tm_ptr;
  7. Time_t the_time;
  8. (Void) time (& the_time );
  9. Tm_ptr = gmtime (& the_time );
  10. Printf ("Raw time is % ld/n", the_time );
  11. Printf ("gmtime gives:/n ");
  12. Printf ("date: % 02d/% 02d/% 02d/n ",
  13. Tm_ptr-> tm_year, tm_ptr-> tm_mon + 1, tm_ptr-> tm_mday );
  14. Printf ("time: % 02d: % 02d: % 02d/n ",
  15. Tm_ptr-> tm_hour, tm_ptr-> tm_min, tm_ptr-> tm_sec );
  16. Exit (0 );
  17. }

In addition, the C language has a function ctime () similar to the GetLocalTime method ().

For localtime (), the prototype is: struct tm * localtime (const time_t * timep); change the gmtime of the test program to localtime, you can see that the output time is the time and date for obtaining. To get a more friendly time and date, you can use the asctime or ctime function for output like date. The prototype is char * ctime (const time_t * timeval). The test code is as follows:

[C-sharp]View plaincopyprint?
  1. # Include <time. h>
  2. # Include <stdio. h>
  3. # Include <stdlib. h>
  4. Int main ()
  5. {
  6. Time_t the_time;
  7. Time (& the_time );
  8. Printf ("The date is: % s/n", ctime (& the_time ));
  9. Exit (0 );
  10. }

3. You can use

BOOL QueryPerformanceFrequency (LARGE_INTEGER * lpFrequency)

BOOL QueryPerformanceCounter (LARGE_INTEGER * lpPerformanceCount) gets the counter value

Then, divide the difference between two counters by Frequency to get the time.

The test procedure is as follows:

[C-sharp]View plaincopyprint?
  1. # Include <iostream>
  2. # Include <windows. h>
  3. Using namespace std;
  4. Int main ()
  5. {
  6. LARGE_INTEGER m_nFreq;
  7. LARGE_INTEGER m_nBeginTime;
  8. LARGE_INTEGER nEndTime;
  9. QueryPerformanceFrequency (& m_nFreq); // get the clock cycle
  10. QueryPerformanceCounter (& m_nBeginTime); // get the clock count
  11. Sleep (100 );
  12. QueryPerformanceCounter (& nEndTime );
  13. Cout <(double) (nEndTime. QuadPart-m_nBeginTime.QuadPart)/m_nFreq.QuadPart <endl;
  14. }

Note that the result must be forcibly converted to double, or the following error is returned: <is ambiguous. The unit of time obtained here is S.

4,TimeGetTime().

Precision: milliseconds, equivalent to GetTickCount. Windows. h is required for use, and Winmm. lib is added. (although the information is found to include mmsystem. h, it is verified that it does not need to be included ). The test code is as follows:

[C-sharp]View plaincopyprint?
  1. # Include <iostream>
  2. # Include <windows. h> // GetTickCount
  3. // # Include <mmsystem. h>
  4. Using namespace std;
  5. Int main ()
  6. {
  7. DWORD start = timeGetTime ();//
  8. Sleep (1000 );
  9. DWORD end = timeGetTime ();//
  10. Cout <timeGetTime () <endl;
  11. Return 0;
  12. }

5. In MFC,CTime: GetCurrentTime () is accurate to seconds and test code is not listed.

 

 

I will not summarize the usage of timer or something.

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.