TIME FUNCTIONS IN VC

Source: Internet
Author: User
Tags sleep function

There are many posts on the career issues of time functions in Vc in the Forum. The following is a summary based on the discussion results and related knowledge.

First, let's talk about how to delay in the program. The latency methods are as follows:
Method 1:
When the sleep function is used, the minimum unit is 1 ms. For example, if the delay is 2 seconds, sleep (2000) is used ).
Method 2:
The disadvantage of using the sleep function is that it cannot process other messages during the period. If the time is too long, it will be like a crash, so we use

The coledatetime and coledatetimespan classes are combined with the Windows message processing process to achieve latency:
Coledatetime start_time = coledatetime: getcurrenttime ();
Coledatetimespan end_time = coledatetime: getcurrenttime ()-start_time;
While (end_time.gettotalseconds () <= 2) // implement latency of 2 seconds
{
MSG? MSG;
Getmessage (& MSG, null, 0, 0 );
Translatemessage (& MSG );
Dispatchmessage (& MSG );
End_time = coledatetime: GetCurrentTime-start_time;
} // In this way, we can also process other messages during latency.
Method 3:
You can use the GetTickCount () function. The return value of this function is DWORD, indicating the time interval after the computer starts in milliseconds.
DWORD dwStart = GetTickCount ();
DWORD dwEnd = dwStart;
Do
{
MSG? Msg ;?
GetMessage (& msg, NULL, 0, 0 );?
TranslateMessage (& msg );
DispatchMessage (& msg );
DwEnd = GetTickCount ();?
} While (dwEnd-dwStart )? <=? 2000 );
The above method is not able to meet our requirements in terms of latency accuracy. Below is a more accurate microsecond-level latency:
LARGE_INTEGER litmp;
LONGLONG QPart1, QPart2;
Double d = 0;
QueryPerformanceCounter (& litmp );
// Obtain the Initial Value
QPart1 = litmp. QuadPart;
While (d <40) // The time you want
{
QueryPerformanceCounter (& litmp );
Qpart2 = litmp. quadpart;
D = (double) (qpart2-qpart1 );
}

Let's take a look at the high-precision timer for Win9x only: queryperformancefrequency () and queryperformancecounter (), which requires the computer to support high-precision timer on hardware. The original form of the function is:
Bool queryperformancefrequency (large_integer * lpfrequency );
Bool queryperformancecounter (large_integer * lpcount );
The data type largeinteger can be an 8-byte long integer or a union structure of two 4-byte long integers. The specific usage depends on whether the compiler supports 64-bit. This type is defined as follows:
Typeef union _ large_integer
{
Struct
{
DWORD lowpart;
Long? Highpart;
};
Longlong quadpart;
} Large_integer;
Call the QueryPerformanceFrequency () function to obtain the clock frequency of the timer. Then, call QueryPerformanceCounter () before and after the occurrence of an event that requires strict timing. the precise time of the event experience can be calculated using the difference in counting and clock frequency obtained twice. The exact duration method of the test function SLEEP (100:
LARGE_INTEGER litmp;
LONGLONG qt1, qt2;
Double dft, dff, dfm;
QueryPerformanceFrequency (& litmp); // obtain the clock frequency
Dff = (double) litmp. QuadPart;
QueryPerformanceCounter (& litmp); // obtain the Initial Value
Qt1 = litmp. QuadPart; Sleep (100 );
QueryPerformanceCounter (& litmp); // obtain the end value.
Qt2 = litmp. QuadPart;
Dfm = (double) (qt2-qt1 );
Dft = dfm/dff; // obtain the corresponding time value
Note that the unit of result for DFT calculation is second.
Reference connection:
Http://dev.csdn.net/develop/article/31/31188.shtm
Http://community.csdn.net/Expert/FAQ/FAQ_Index.asp? Id = 195559

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.