Summary of timing functions under Windows and Linux platforms

Source: Internet
Author: User
Tags bool integer printf sleep time in milliseconds linux

This paper sums up the timer functions commonly used under Windows and Linux platforms, including the functions of three precision of seconds, milliseconds and microseconds.

For example, Windows API-specific functions GetTickCount (), timeGetTime (), and QueryPerformanceCounter (),

Linux platform-specific gettimeofday () functions, as well as standard C + + functions time () and clock (). Here's a brief description of this and a sample code.

General purpose C + + Timer function time () and clock ()

time_t time (time_t *timer);

Returns the number of seconds elapsed in GMT (GMT), from January 1, 1970 00:00:00 to the current moment.

time_t is actually a long integer typedef long time_t;

clock_t clock (void);

Returns the number of CPU clock ticks (clock tick) that elapse when the process starts to invoke the function, called the Wall Clock Time (Wal-clock) in the MSDN, in milliseconds.

Clock_t is actually a long integer typedef long clock_t;

Window platform-specific functions

DWORD timegettime (void);

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.

DWORD WINAPI GetTickCount (void);

This function, like timeGetTime (), returns the system time in milliseconds.

High-precision timing, in microseconds (1 ms =1000 microseconds).

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/

BOOL QueryPerformanceCounter (Large_integer *lpperformancecount); Gets the value of the high-precision timer (if there is such a timer).

BOOL QueryPerformanceFrequency (Large_integer *lpfrequency); Returns the frequency (times per second) of the High-precision counters supported by the hardware, and 0 indicates failure.

The Large_integer is actually a union, can obtain __int64 QuadPart, can also get the low 32-bit DWORD LowPart and the high 32-bit value long highpart respectively.

In use, first use QueryPerformanceFrequency () to get the frequency of the counter, and then calculate two times call QueryPerformanceCounter () the difference between the timer value,

Using the difference to get rid of the frequency is accurate timing.

Linux platform-specific functions

int gettimeofday (struct timeval *tv,struct timezone *tz);

Gets the current exact time (January 1, 1970 to present time) with a precision of microseconds.

Structure to save time

Strut Timeval {

Long tv_sec; Number of seconds

Long tv_usec; Microsecond number

};

Attach Code

  1 #include <iostream> 2 3 #if defined (_WIN32) | |            Defined (WIN32)/**windows*/4 #define WINDOWS_IMPL 5 #include <windows.h> 6 #include <time.h> Time (), clock () 7 #include <Mmsystem.h>//timegettime () 8 #pragma comment (lib, "Winmm.lib")//t Imegettime () 9 #elif defined (__linux__) | | Defined (__apple__) | | Defined (__freebsd__) | | Defined (BSD)/**linux*/#define LINUX_IMPL #include <sys/time.h>//gettimeofday () #endif 13 # Include <stdio.h> 14 15/*********************************************************** 16 General: time_t Time (Tim     e_t *tloc); Returns the number of seconds since 0 o'clock January 1, 1970, with a precision of seconds clock_t clock (): Returns the CPU time taken by the program from boot to function call with a precision of milliseconds, but the general minimum precision is 33ms Windows specific: GETTICKCO UNT (): Returns the number of milliseconds from the operating system boot up to now, with a precision of milliseconds, but the minimum precision is 18ms 22 return value with a 32-bit DWORD to store, so the maximum value that can be stored is 2^32 ms for about 49.71 days, timeget Time (): Returns the system time in milliseconds, which is the number of times elapsed since the system was opened, with a precision of milliseconds queryperformancecounter (): Returns the value of a high precision performance counter, with a precision of subtleBut the smallest unit of exact timing is the system-specific Linux: gettimeofday (struct timeval *tv,struct timezone *tz); Obtains the current precise time (January 1, 1970 to present time), the precision is microsecond ***********************************************************/void Mysleep (
 int sec_time) to {#if defined (Windows_impl) (sec_time*1000);
 #elif defined (Linux_impl) sleep (sec_time); #endif () Test_time () 40 {41//Universal 42////Time () to time_t Timebegin, Timeen
 D
 Timebegin = time (NULL);
 Mysleep (1);
 Timeend = time (NULL);
 To printf ("%d\n", Timeend-timebegin);
 * * Structure used in select () call, taken from the BSD file sys/time.h.         //struct Timeval {//long tv_sec;        /* seconds *//long tv_usec;
 /* and microseconds */55//};
 Timeval Val;
 57 58//With clock () to time milliseconds clock_t clockbegin, clockend; Clockbegin = Clock ();
 Mysleep (1);
 Clockend = Clock ();
 printf ("%d\n", Clockend-clockbegin);
 #ifdef Windows_impl//windows 67 68//GetTickCount () to time milliseconds dwgtcbegin, dwgtcend;
 Dwgtcbegin = GetTickCount ();
 Sleep (1000);
 Dwgtcend = GetTickCount ();
 printf ("%d\n", Dwgtcend-dwgtcbegin);
 74 75//With timeGetTime () to time milliseconds dwbegin DWORD, dwend;
 Dwbegin = timeGetTime ();
 Sleep (1000);
 Dwend = timeGetTime ();
 -printf ("%d\n", Dwend-dwbegin);
 81 82//With QueryPerformanceCounter () to time microsecond large_integer Large_interger;
 Double DFF;
 __int64 C1, C2;
 QueryPerformanceFrequency (&large_interger); DFF = Large_interger.
 QuadPart;
 QueryPerformanceCounter (&large_interger); C1 = Large_interger.
 QuadPart;
 Sleep (1000);
 QueryPerformanceCounter (&large_interger); C2 = Large_interger.
QuadPart; %lf\n ("High-precision timer frequency", DFF);
 ("First timer value%I64D second timer value%i64d timer difference%i64d\n", C1, c2, C2-C1);
 printf ("Timed%lf milliseconds", (C2-C1) * 1000/DFF);
#elif defined (linux_impl)//linux struct timeval tpstart,tpend;
Double Timeuse;
102 Gettimeofday (&tpstart,null);
Sleep (1);
Gettimeofday (&tpend,null); timeuse=1000000* (tpend.tv_sec-tpstart.tv_sec) +tpend.tv_usec-tpstart.tv_usec;//Note that both the seconds and microsecond readings should be counted in the
"Used time:%fus\n", timeuse); #endif 108 109} $ int main () 112 {113 Test_time (); 114 GetChar (); 0; 116}

The results of running on the Windows platform are as follows:

The results are as follows on the Linux platform:

Author: cnblogs Lizhenghn

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.