Windows Time Functions

Source: Internet
Author: User
Tags sleep function

Introduction



We are measuring the running time of a function, or judgingAlgorithmTime efficiency, orProgramWe need a timer to regularly execute a specific operation, such as in multimedia, such as in the game medium,
All use time functions. 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 operating system provide us with a lot of time
The precision of these time functions is also different. Therefore, if we want to obtain accurate results, we must use appropriate time functions. Now I will introduce several common time letters in windows.
Number.

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. After the specified interval, start the thread and continue execution.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 precision of sleep functions is very low. When the system is busy, its precision is 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: timer event under MFC



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: 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: timer events have a precision range of millimeters. The more busy the system is, the worse the accuracy is.

3: Time in C Language



Use: 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: coledatetime and coledatetimespan classes in COM objects


Use: 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: The latency is 2 seconds. In these two seconds, we can call dosomething () cyclically to process other functions or messages during the delay.
Coledatetime, coledatetimespan is the application of ctime and ctimespan in MFC in COM.
Ctime and ctimespa are also valid.



Precision: Second Level

5: clock cycle clock () in C Language ()



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 is started.


Precision: Ms level. For a short period of time, the timing or delay can reach Ms level. For a long period of time, the timing or delay accuracy is not enough. In Windows, clocks_per_sec is 1000.

6: gettickcount () in Windows ()

Use: 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.


It is not the actual execution time of a function or algorithm, because it is impossible for the function and algorithm thread to occupy the CPU all the time. It is the same for all functions that determine the execution time, but it is basically accurate, you can perform regular queries. The gettickcount () and clock () functions require real
Time Clock time, with interruptions and latency issues.

Precision: WindowsNT
The precision of 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: timegettime in Windows


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: timesetevent in Windows


Use: 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 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 )//
Flag parameter, time_oneshot: execution once; time_periodic: Periodical execution



You can call the timesetevent () function to define the tasks to be periodically executed in
In the lpfunction callback function (such as scheduled sampling and control), the events to be processed are completed. 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: millisecond. 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 function queryperformancefrequency, queryperformancecounter

Use: large_integer m_nfreq;



Large_integer m_nbegintime;



Large_integer nendtime;



Queryperformancefrequency (& m_nfreq );//
Get the 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 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 are used differently, therefore, their precision is different, so if a simple latency can be implemented using the sleep function, a slightly accurate latency can enable
to use the clock function, the gettickcount function, more advanced and practical timegettime functions; timer can be used for simple scheduled events, and
timesetevent can be used accurately; or the general system time can be used through time, ctime, or coledatetime, you can use clock, the
gettickcount function, or the timegettime function to obtain the accurate time. You must use the hardware-supported
queryperformancefrequency function and queryperformancecounter function to obtain the accurate time.

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.