Windows system can use time (), clock (), timeGetTime (), GetTickCount (), QueryPerformanceCounter () to time a piece of program code
#include <stdio.h>
#include <windows.h>
#include <time.h>//time_t time () clock_t clock ()
#include <Mmsystem.h>//timegettime ()
#pragma comment (lib, "Winmm.lib")//timegettime ()
How to: Replace the sleep () function with a function that requires a test run time.
int main ()
{//Time with time (), in seconds
time_t Timebegin, Timeend;
Timebegin = time (NULL);
Sleep (1000);
Timeend = time (NULL);
printf ("%d\n", Timeend-timebegin);
Use clock () to timing, in milliseconds
clock_t Clockbegin, Clockend;
Clockbegin = Clock ();
Sleep (800);
Clockend = Clock ();
printf ("%d\n", Clockend-clockbegin);
Use timeGetTime () to timing, in milliseconds
DWORD Dwbegin, Dwend;
Dwbegin = timeGetTime ();
Sleep (800);
Dwend = timeGetTime ();
printf ("%d\n", Dwend-dwbegin);
//Timing with GetTickCount (), in milliseconds
dword dwgtcbegin, dwgtcend;
Dwgtcbegin = GetTickCount ();
Sleep (800);
dwgtcend = GetTickCount ();
printf ("%d\n", dwgtcend-dwgtcbegin);
Use QueryPerformanceCounter () to timing, in microseconds
Large_integer Large_interger;
Double DFF;
__int64 C1, C2;
QueryPerformanceFrequency (&large_interger);
DFF = Large_interger. QuadPart;
QueryPerformanceCounter (&large_interger);
C1 = Large_interger. QuadPart;
Sleep (800);
QueryPerformanceCounter (&large_interger);
C2 = Large_interger. QuadPart;
printf ("Native high-precision timer frequency%lf\n", DFF);
printf ("First timer value%i64d\n second timer value%i64d\n timer difference%i64d\n", C1, c2, C2-C1);
printf ("Timed%lf milliseconds \ n \ \" (C2-C1) * 1000/DFF);
return 0;
}