A summary of the usual timing methods in C language
Original Address
1. Time ()
header file : time.h
function prototype : time_t time (time_t * timer)
function : Returns the number of seconds elapsed in GMT (GMT), from January 1, 1970 00:00:00 to the current moment.
Use the time () function to combine other functions (such as localtime, Gmtime, Asctime, CTime) to obtain the current system or standard time.
You can use the Difftime function to calculate the difference between two time_t types of time, which can be used for timing. Using Difftime (T2,T1) is more accurate than t2-t1, because the C standard does not stipulate that the unit of time_t must be seconds, and Difftime will be converted according to the machine, more reliable.
usage :
[CPP] view plain copy print? time_t start,end; Start =time (NULL);//or time (&start); ... calculating end =time (NULL); printf ("time=%d\n", Difftime (End,start)); Summary : C standard library functions, the best portability, performance is also very stable, but the precision is too low, can only be accurate to the second, for the general event timing is sufficient, and the time of calculation is obviously not enough. 2. Clock ()
header file : time.h
function prototype : clock_t clock (void);
function : The return value of this function is the hardware tick number, to be converted to seconds, which needs to be divided by clk_tck or clk_tckclocks_per_sec. For example, under vc++6.0, the values of these two quantities are 1000.
usage :
[CPP] view plain copy print? clock_t Start,end; start = Clock (); ... calculating. End = Clock (); printf ("time=%f\n", (double) end-start)/CLK_TCK); Summary : can be accurate to milliseconds, suitable for use in general situations. 3. timeGetTime ()
Win32API
header file : Mmsystem.h Reference Library : Winmm.lib
function prototype : DWORD timegettime (VOID);
function : Returns the system time in milliseconds. The system time is the number of milliseconds elapsed since the system was booted to the calling function. Note that this value is 32-bit and will cycle between 0 and 2^32, about 49.71 days.
usage :
[CPP] view plain copy print? Dwordstart,end; Start= timeGetTime (); ... calculating ... end= timegettime (); printf ("time=%d\n", End-start); Summary : The time precision of the function is five milliseconds or greater, depending on the performance of the machine. The Timebeginperiod and Timeendperiod functions can be used to improve the precision of the timeGetTime function. If used, the timeGetTime function is called continuously, and the difference between a series of return values is determined by timebeginperiod and Timeendperiod. 4. GetTickCount ()
Win32API
header file : windows.h
function prototype : DWORD WINAPI GetTickCount (void);
function : Returns the number of milliseconds since the device was started (excluding system pause time).
usage :
[CPP] view plain copy print? Dwordstart,end; Start= GetTickCount (); ... calculating ... end= gettickcount (); printf ("time=%d\n", End-start); Summary : accurate to milliseconds. For the general real-time control, using the GetTickCount () function can meet the precision requirements. 5. QueryPerformanceCounter (), QueryPerformanceFrequency ()
Win32API
header file : windows.h
function prototype : Boolqueryperformancecounter (Large_integer *lpperformancecount);
Boolqueryperformancefrequency (Large_integer *lpfrequency);
function : The former gets the number of clock cycles that the CPU has performed since it was powered on. The latter is used to get your machine to perform how many times a second, which is your clock cycle.
Add : Large_integer can be either a 8-byte long integer or a joint structure of two 4-byte integer numbers, depending on whether the compiler supports 64 digits:
[CPP] view plain copy print? typedef Union_large_integer {struct {DWORD lowpart; LONG Highpart; }; Longlong QuadPart; }large_integer;
usage :
Before timing, call the QueryPerformanceFrequency () function to obtain the clock frequency of the internal timer of the machine, and then call the QueryPerformanceCounter () function separately before and after the event that requires strict timing. The exact time of the event experience is calculated using the difference of two counts and the clock frequency.
[CPP] view plain copy print?