Windows Time (zz)

Source: Internet
Author: User
Tags sleep function
This section describes how to measure the running time of a function, determine the time efficiency of an algorithm, or require a timer in a program to regularly execute a specific operation, such as in multimedia, for example, in games, time functions are used. For example, we record the start time and end time of a function or algorithm, and then use the difference between the two to get the running time of the function or algorithm. The compiler and the operating system provide us with a lot of time functions. The precision of these time functions is also different. Therefore, if we want to get accurate results, we must use appropriate time functions. Now I will introduce several common time functions in windows. 1 : Sleep Function Usage:Sleep (1000): In Windows and Linux, 1000 represents different meanings. In Windows, 1000 is represented in milliseconds, that is, 1 second. in Linux, 1000 is represented in seconds, in Linux, usleep can be used for millisecond-level functions. Principle:The sleep function sleep the thread that calls the sleep function, and the thread takes the initiative to discard the time slice. After the specified interval, start the thread and continue executing the code. The Sleep function does not act as a scheduled function. Its main function is latency. Sleep (0) may be displayed in some multithreading; its main purpose is to allow time slice. Precision:The sleep function has a very low precision. When the system is busy, its precision will be lower. Sometimes we sleep for 1 second, maybe 3 seconds before execution can continue. Its Precision depends on the thread priority, the priority of other threads, and the number of threads. 2 : MFC Under Timer Event Usage:1. call the SetTimer () function to set the time interval. For example, SetTimer (0,100, NULL) sets the time interval of 100 milliseconds. 2. add the scheduled response function OnTimer () to the application, and add the Response Processing statement to the function to complete the operation at that time. Principle:The same as the sleep function. The difference is that timer is a timer. You can specify the callback function. The default value is OnTimer. Precision:The timer event's precision range is in millimeters, and the more busy the system is, the less accurate it is. 3 : C Language Time Usage:Time_t t; time (& t); The Time function is used to obtain the current time. Principle:The time function is mainly used to obtain the current time. For example, if we create an electronic clock program, we can use this function to obtain the current time of the system. Precision:Second level 4 : COM Object COleDateTime , COleDateTimeSpanClass Usage:COleDateTime start_time = COleDateTime: GetCurrentTime (); COleDateTimeSpan end_time = COleDateTime: GetCurrentTime ()-start_time;
While (end_time.GetTotalSeconds () <2)
{
// Other messages can be processed in a delayed or scheduled period
DoSomething ()
End_time = COleDateTime: GetCurrentTime-start_time ;} Principle:These two seconds indicate the delay of 2 seconds. In these two seconds, we can call DoSomething () cyclically, so that we can also process other functions or messages during the delay. COleDateTime and COleDateTimeSpan are the applications of CTime and CTimeSpan in MFC in COM. Therefore, the above method is equally effective for CTime and CTimeSpa. Precision:Second level 5 : C Language clock cycle Clock () Usage:Clock_t start = clock ();
Sleep (100 );
Clock_t end = clock ();
Double d = (double) (start-end)/CLOCKS_PER_SEC; Principle:Clock () is the time interval after the computer is started. Precision:For a short period of time, the timing or latency can reach the ms level. For a long period of time, the timing or delay accuracy is not enough. In windows, CLOCKS_PER_SEC is 1000. 6 : Windows Under Gettickcount () Usage:DWORD start = GetTickCount ();
Sleep (100 );
DWORD end = GetTickCount (); Principle:GetTickCount () is the time interval after the system is started. You can determine the execution time of the function by entering the function start time and by the end time of the function exit. This time is not the actual execution time of the function or algorithm, because it is impossible for the function and algorithm thread to occupy the CPU all the time, all the functions that determine the execution time are the same, but they are basically accurate and can be scheduled through queries. The GetTickCount () and Clock () functions ask the motherboard BIOS for real time clock time, which may cause interruptions and delay problems. Precision:The accuracy of WindowsNT 3.5 and later versions is 10 ms, and its time precision is higher than that of clock functions. GetTickCount () is often used in multimedia. 7 : Windows Timegettime Usage: You need to include mmsystem. H, windows. H, and add the static library winmm. Lib.TimeBeginPeriod (1 );
DWORD start = timeGetTime ();
Sleep (100 );
DWORD end = timeGetTime ();
TimeEndPeriod (1 ); Principle: timegettime is often used in multimedia timers and can be scheduled through queries. Timing through queries also affects the timer precision. Precision: milliseconds, equivalent to gettickcount. However, compared with gettickcount, timegettime can use timebeginperiod and timeendperiod to set the minimum resolution precision of the timer. timebeginperiod and timeendperiod must appear in pairs. 8 : Windows TimeseteventUse: Do you still remember the Timer under VC? Timer is a Timer, and we mentioned several time functions or types above. The Timer function can only be implemented through round training, that is, you must create another thread for separate processing, which will affect the timing precision, fortunately, windows provides the built-in timer timeSetEvent, and the function prototype is MMRESULT timeSetEvent (UINT uDelay, // specify the event cycle in milliseconds
UINT uResolution, // specify the latency precision in milliseconds. The smaller the value, the higher the timer event resolution. The default value is 1 ms.
LPTIMECALLBACK lpTimeProc, // point to a callback function
WORD dwUser, // stores the callback data provided by the user
UINT fuEvent) // The flag parameter, TIME_ONESHOT: Run Once; TIME_PERIODIC: You can call the timeSetEvent () function to periodically execute a specific application, define the tasks to be periodically executed in the lpFunction callback function (such as scheduled sampling and Control) to complete the events to be processed. Note that the task processing time cannot be greater than the cycle interval. In addition, after the timer is used, call timeKillEvent () to release it in time. Principle:It can be understood as the timeGetTime of the callback function. Precision:In milliseconds, timeSetEvent can use timeBeginPeriod and timeEndPeriod to set the minimum resolution precision of the timer. timeBeginPeriod and timeEndPeriod must appear in pairs.
9 : High-precision time control functions QueryPerformanceFrequency , QueryPerformanceCounter Usage:LARGE_INTEGER m_nFreq;
LARGE_INTEGER m_nBeginTime;
LARGE_INTEGER nEndTime;
QueryPerformanceFrequency (& m_nFreq); // get the clock cycle
QueryPerformanceCounter (& m_nBeginTime); // get the clock count
Sleep (100 );
QueryPerformanceCounter (& nEndTime );
Cout <(nEndTime. QuadPart-m_nBeginTime.QuadPart) * 1000/m_nFreq.QuadPart <endl; Principle:There is also a counter on the CPU, in the unit of the machine's clock, can be read through rdtsc, without interruption, so its accuracy is equivalent to the system time. Precision:The computer obtains hardware support and has a high precision. It can be used to determine the precision range of other time functions. 10 Summary:The nine commonly used time functions mentioned above have different precision because of their different uses. Therefore, if the latency is simple, you can use the sleep function, for a slightly accurate latency, you can use the clock function, the GetTickCount function, and the timeGetTime function. Timer can be used for simple scheduled events, and timeSetEvent can be used accurately; you can use the clock, GetTickCount, or timeGetTime function to obtain the accurate time through time, CTime, or COleDateTime, to obtain accurate system time, use the hardware-supported QueryPerformanceFrequency function and QueryPerformanceCounter function.

 

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.