Summary of methods for acquiring time and calculating the difference between C and C + + under Windows

Source: Internet
Author: User
Tags sleep function time and date

A summary of several methods for obtaining time and calculating difference between windows and C + +

One, standard C and C + + are available

1, get time with time_t times (time_t * timer), calculate the difference between using double

Difftime (time_t timer1, time_t Timer0). Accurate to seconds.

The test procedure is as follows:

1#include <time.h>2#include <stdio.h>3 4 intMain ()5 {6 time_t start, end;7     DoubleCost ;8     9Time (&start);TenSleep1); OneTime (&end); Acost=Difftime (end,start); -printf"%f\n", cost); -      the     return 0; -}

This procedure is passed in fedora9 test.

With regard to the sleep function in the code, it is important to note that:

1) under Windows, the Sleep () function, and the need to include the Windows.h

2) about the number of sleep, under Windows and Linux 1000 means that the meaning is not the same, under Windows 1000 milliseconds, that is 1 seconds, Linux for 1000 seconds, Linux under the use of millisecond-level functions can use Usleep.

2, clock_t clock ()

Clock () Gets the time interval after the computer starts, gets the CPU time, accurate to

1/clocks_per_sec seconds.

The test procedure is as follows:

1#include <time.h>2#include <stdio.h>3 4 intMain ()5 {6     Doublestart, end, cost;7     8start=clock ();9Sleep1);TenEnd=clock (); Onecost=end-start; Aprintf"%f\n", cost); -      -     return 0; the}

In C + + (here for Windows environments, Linux and Windows are available in standard C)

1, GetTickCount ()

The calling function must contain windows.h. Get the system to run the time accurate to milliseconds, the test program is as follows:

1#include <iostream>2#include <windows.h>3 4 using namespacestd;5 6 intMain ()7 {8     DoubleStart =GetTickCount ();9Sleep ( +);Ten     DoubleEnd=GetTickCount (); Onecout <<"GetTickCount:"<< End-start <<Endl; A      -     return 0; -}

2, Getlocaltime ()

Obtained is the structure of the year,month, such as the preservation of information. The C language Time function gets the number of seconds from January 1, 1970 0:0 to 0 seconds. The Gmtime function needs to be converted to a common calendar (the world time is returned, and the LocalTime function is used to show the time that is common).

In C, the structure of a common calendar is a struct TM, contained in the time.h, the C + + language is the SYSTEMTIME structure, contained in the Winbase.h (programmed to include Windows.h). Of course, the precision must be seconds.

The test procedure is as follows:

1#include <iostream>2#include <windows.h>3 4 using namespacestd;5 6 intMain ()7 {8SYSTEMTIME start;//in Windows.h9Getlocaltime (&start);//time.h the same effect as the TM structureTencout<< Start.year <<Endl; One}

The sample code for the C language Gmtime method is as follows:

1#include <time.h>2#include <stdio.h>3#include <stdlib.h>4 5 intMain ()6 {7     structTM *tm_ptr;8 time_t the_time;9(void) Time (&the_time);Ten      OneTm_ptr = Gmtime (&the_time); Aprintf"Raw Time is%ld\n", the_time); -printf"gmtime gives:\n"); -printf"Date:%02d/%02d/%02d\n", Tm_ptr->tm_year, tm_ptr->tm_mon+1, tm_ptr->tm_mday); theprintf"Time :%02d:%02d:%02d\n", Tm_ptr->tm_hour, Tm_ptr->tm_min, tm_ptr->tm_sec); -Exit0); -}

In addition, the C language functions like the Getlocaltime method are CTime ().

For LocalTime (), the prototype is: struct TM *localtime (const time_t *TIMEP); Change the test program's gmtime to LocalTime, you can see the output time for time and date. To be more friendly with time and date, output as date, you can use Asctime or CTime function, prototype: Char *ctime (const time_t *timeval);

The test procedure is as follows:

1#include <time.h>2#include <stdio.h>3#include <stdlib.h>4 5 intMain ()6 {7 time_t the_time;8Time (&the_time);9printf"The date is:%s \ n", CTime (&the_time));Ten      OneExit0); A}

3, to obtain high-precision time, you can use

BOOL QueryPerformanceFrequency (Large_integer *lpfrequency) Gets the frequency of the counter of the system

BOOL QueryPerformanceCounter (Large_integer *lpperformancecount) Gets the value of the counter

Then divide the difference of two times by dividing the frequency to get the time.

The test procedure is as follows:

#include <iostream>#include<windows.h>using namespacestd;intMain () {Large_integer m_nfreq;    Large_integer M_nbegintime;        Large_integer Nendtime; QueryPerformanceFrequency (&AMP;M_NFREQ);//Get clock cycleQueryPerformanceCounter (&m_nbegintime);//Get clock CountSleep ( -); QueryPerformanceCounter (&nendtime); cout<< (Double) (Nendtime.quadpart-m_nbegintime.quadpart) * +/m_nfreq.quadpart <<Endl;}

It is important to note that the result needs to be cast to double, otherwise it will get the following error:<< is ambiguous.

4, timeGetTime ().

Accuracy: milliseconds, equivalent to GetTickCount (). Use need to include windows.h, and add Winmm.lib (although the data said need to include mmsystem.h, but the experience can not be included).

The test procedure is as follows:

1#include <iostream>2#include <windows.h>3 //#include <mmsystem.h>4 5 using namespacestd;6 7 intMain ()8 {9DWORD start =timeGetTime ();TenSleep ( +); OneDWORD end=timeGetTime (); Acout << End-start <<Endl; -      -     return 0; the}

C and C + + a summary of how time is obtained under Windows and how the difference is calculated

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.