One, standard C and C + + are available
1. Get time with time_t times (time_t * timer), calculate the difference using double difftime (time_t timer1, time_t Timer0). Accurate to seconds.
The test procedure is as follows:
#include <time.h><stdio.h>int main () { time_t start, end; Double Cost ; Time (&start); Sleep (1); Time (&end); cost =difftime (end,start); printf ("%f/n", cost); 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 contains 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, and gets the CPU time, accurate to 1/clocks_per_sec seconds.
The test procedure is as follows:
#include <time.h><stdio.h>int main () { double start,end, Cost; Start=clock (); Sleep (1); End=clock (); cost =end-start; printf ("%f/n", cost); return 0 ;}
In C + + (here for Windows environments, Linux and Windows are available in standard C)
1,GetTickCount ()
The
Call function must contain windows.h. Get the system to run the time accurate to milliseconds, the test program is as follows:
#include <iostream> #include <windows.h>using namespace Std; int Main () { double start = GetTickCount (); Sleep ( 1000 ); double end=gettickcount (); cout << gettickcount: Span style= "color: #800000;" > " << end-start << Endl; 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:
#include <iostream><windows.h>usingnamespace std; int Main () { //windows.h getlocaltime (&start); time.h The same effect as the TM structure cout<< start.year << Endl;}
The sample code for the C language Gmtime method is as follows:
#include <time.h>#include<stdio.h>#include<stdlib.h>intMain () {structTM *tm_ptr; time_t the_time; (void) Time (&the_time); Tm_ptr= Gmtime (&the_time); printf ("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); printf ("Time :%02d:%02d:%02d/n", Tm_ptr->tm_hour, Tm_ptr->tm_min, tm_ptr->tm_sec); Exit (0);}
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 test program's gmtime to LocalTime, you can see the output time for time and date. To get the time and date more friendly, output as date, you can use the Asctime or CTime function, prototype: Char *ctime (const time_t *timeval), and test code as follows:
#include <time.h><stdio.h><stdlib.h>int main () { time_t the_ Time; Time (&the_time); printf ("Thedate is:%s/n" , CTime (&the_time)); Exit (0);}
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 (&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 code is as follows:
#include <iostream> #include <windows.h>// gettickcount // #include <mmsystem.h> using namespace Std; int main () {DWORD start = timeGetTime (); // Sleep (1000 ); DWORD End = timeGetTime (); // cout << timegettime () << Endl; return 0 ;}
5, MFC,CTime:: GetCurrentTime () accurate to the second, do not list the test code.
About the timer what, the current use of the place is not much, it does not summarize
http://blog.csdn.net/coder_xia/article/details/6566708
A summary of several methods of "reprint" C + + under Windows to obtain time and calculate difference