Windows Time Functions

Source: Internet
Author: User
Tags sleep function

Introduced
We are measuring a function run time, or determine the time efficiency of an algorithm, or in the program we need a timer, timed to perform a specific operation, such as in the multimedia, such as in the game medium, will use the time function. For example, we can use the difference between a function or an algorithm to calculate the elapsed time by recording the start and the time of the function or algorithm. The compiler and the operating system provide us with a lot of time functions, and the accuracy of these time functions is different, so if we want to get accurate results, we must use the appropriate time function. Now I'm going to introduce several common time functions under Windows.
1:Sleepfunction
use:Sleep (1000), under Windows and Linux 1000 means that the meaning is not the same, Windows represents 1000 milliseconds, that is 1 seconds, Linux for 1000 seconds, Linux under the use of millisecond-level functions can use Usleep.
principle:The sleep function is the thread that calls the sleep function sleeps, and the thread abandons the time slice voluntarily. When the thread is started after the specified interval, continue executing the code. Sleep function does not play the role of timing, the main role is delay. Sleep (0) may be seen in some multi-threading, and its main purpose is to make time slices.
accuracy:The accuracy of the sleep function is very low, and the more busy the system it is, the lower the accuracy, and sometimes we hibernate for 1 seconds, maybe 3 seconds before we can continue to execute. Its accuracy depends on factors such as the priority of the thread itself, the priority of other threads, and the number of threads.
2:MFCunder theTimerEvents
use:1. Call the function SetTimer () to set the timing interval, such as SetTimer (0,100,null), which is the time interval for setting 100 milliseconds, 2. Add a timer response function OnTimer () to the application, and add a processing statement for the response in the function. Used to complete the time-to-hour operation.
principle:Same as the sleep function. The difference is that the timer is a timer, you can specify the callback function, the default is the OnTimer () function.
accuracy:The accuracy of the timer event is at the millimeter level, and the more busy the system is, the worse the accuracy is.
3:Clanguage under the Time
use:time_t t;time (&t); The time function is to get the current times.
principle:The time function is mainly used to get the current time, such as we do an electronic clock program, you can use this function, to obtain the current system.
accuracy:Second level
4: In a COM objectCOleDateTime, COleDateTimeSpan class
    use:COleDateTime start_time = Coledatetime::getcurrenttime ();
COleDateTimeSpan end_time = Coledatetime::getcurrenttime ()-start_time;
while (End_time. Gettotalseconds () < 2)
{
Processing delay or timing can process other messages
DoSomething ()
End_time = Coledatetime::getcurrenttime-start_time;
}
principle:The above represents a delay of 2 seconds, and in this two seconds we can loop call dosomething (), so that we can also handle other functions, or messages at the time of delay. Coledatetime,coledatetimespan is MFC CTime, CTimeSpan in the application of COM, so, the above method for CTime, Ctimespa is also effective.
       accuracy:Second level
5:Cclock Cycles in languageclock ()
use: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 starts.
accuracy:Ms level, for a short period of time or delay can reach the MS level, for a longer period of time or delay accuracy is not enough. Under Windows Clocks_per_sec is 1000.
6:Windowsunder theGetTickCount ()
use:DWORD start = GetTickCount ();
Sleep (100);
DWORD end = GetTickCount ();
principle:GetTickCount () is the time interval after the system starts. By entering the function to start the timing, to exit the function to end the timing, so that the function can be judged the execution time, this time is not a function or the actual execution time of the algorithm, because the function and algorithm thread can not always occupy the CPU, for all the function to judge the execution time is the same, but basically is very accurate, Can be timed through a query. The GetTickCount () and clock () functions are to the motherboard BIOS to real time Clock times, there will be interrupt generation, and latency issues.
accuracy:WindowsNT 3.5 and later version accuracy is 10ms, its time accuracy is higher than the clock function, GetTickCount () is often used in multimedia.
7:WindowsNext timeGetTime
use: Need to include Mmsystem.h, Windows.h, add Static library Winmm.lib.
Timebeginperiod (1);
DWORD start = timeGetTime ();
Sleep (100);
DWORD end = timeGetTime ();
Timeendperiod (1);
principle: timeGetTime is also often used in multimedia timers, which can be timed by query. Timing through the query itself will also affect the timing accuracy of the timer.
accuracy: milliseconds, equivalent to GetTickCount (). However, compared with GetTickCount, timeGetTime can set the minimum resolution accuracy of the timer through Timebeginperiod,timeendperiod, timebeginperiod,timeendperiod must appear in pairs.
8: Under Windowstimesetevent
Use: Also remember the VC under the timer? Timer is a timer, and the above we mentioned a number of time functions or types, the implementation of the timing function can only be achieved through rotation, that is, the need to create a separate thread processing, which will affect the timing accuracy, fortunately, Windows provides built-in timer timesetevent, function prototype for
Mmresult timesetevent (UINT udelay,//Specify the period of the event in milliseconds
UINT uresolution,//Specify the precision of the delay in milliseconds, the smaller the value the higher the timer event resolution. Default value is 1ms
Lptimecallback Lptimeproc,//point to a callback function
WORD Dwuser,//store user-supplied callback data
UINT fuevent)//flag parameter, Time_oneshot: Execute once; Time_periodic: Periodic execution
For specific applications, you can complete the desired event by calling the timeSetEvent () function and defining the tasks that need to be executed periodically in the lpfunction callback function (e.g., timed sampling, control, etc.). It is important to note that the task processing time cannot be greater than the period interval. In addition, after the timer is used, the timekillevent () should be called in time to release it.
principle:Can be understood as the timegettime of the generation callback function
accuracy:In milliseconds, the timesetevent can set the minimum resolution accuracy of the timer through Timebeginperiod,timeendperiod, and timebeginperiod,timeendperiod must appear in pairs.
9: High-precision time-control functionQueryPerformanceFrequency,QueryPerformanceCounter
use:Large_integer M_nfreq;
Large_integer M_nbegintime;
Large_integer Nendtime;
QueryPerformanceFrequency (&m_nfreq); Get clock cycle
QueryPerformanceCounter (&m_nbegintime); Get 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 machine's clock, can be read through the RDTSC, without interruption, so its accuracy and system time is equivalent.
accuracy:The computer obtains the hardware support, the precision is high, may judge the other time function the precision range through it.
TenSummary:The above mentioned 9 kinds of time functions commonly used, due to their different uses, so their accuracy is not the same, so if the simple delay can be used sleep function, a slightly accurate delay can use the clock function, GetTickCount function, more advanced practical timegettime function Simple timing events can be used with a timer, accurately can be used timesetevent, or the general system time can be CTime, or coledatetime, to obtain accurate time can be used clock, or GetTickCount function, or the timeGetTime function, while obtaining an accurate system time to use hardware-supported queryperformancefrequency functions, QueryPerformanceCounter functions.

http://blog.csdn.net/adcxf/article/details/5288912

Windows Time Functions

Related Article

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.