VC + + program run time test function

Source: Internet
Author: User
Tags sleep function

0: Introduction

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:sleep function

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 actively. 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 sleep function has a very low precision, and when the system gets busier it is less accurate, and sometimes we hibernate for 1 seconds and maybe 3 seconds before we can continue. Its accuracy depends on factors such as the priority of the thread itself, the priority of other threads, and the number of threads.

Timer events under 2:MFC

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 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: Time in C language

Use: time_t time (time_t * timer): Use only the C standard library (accurate to seconds) when the function is to get the current times.

Rationale: 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

The Coledatetime,coledatetimespan class in the 4:com object

Use:

COleDateTime start_time == Coledatetime::getcurrenttime ()-2) {  //  Processing delay or timing period can handle other messages  = coledatetime::getcurrenttime-start_time;}  

Principle: The above represents a delay of 2 seconds, and this two seconds we can loop call dosomething (), so that we can also handle the delay when the other functions, or messages. Coledatetime,coledatetimespan is the application of Ctime,ctimespan in COM in MFC, so the above method is also valid for Ctime,ctimespa.

Accuracy: Second Level

Clock cycles in 5:c language clocks ()

Use:

clock_t start = clock (); Sleep (= clock (); double d = (double) (start-end)/clocks_per_sec;

Principle: Clock () is the time interval to get the computer to start.

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:windows under the GetTickCount ()

Use:

DWORD start = GetTickCount (); Sleep (= 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:windows under timeGetTime

Use: Need to include mmsystem.h,windows.h, add static library Winmm.lib.

Timebeginperiod (1= timegettime (); Sleep (= 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:windows under the timeSetEvent

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, must create a separate thread processing, which will affect the timing accuracy, fortunately, Windows provides built-in timer timesetevent, The function prototypes are:

// Specify the period of the event in milliseconds // Specify the precision of the delay in milliseconds, the smaller the value, the higher the timer event resolution. The default value is 1ms// point to a callback function // 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: milliseconds, timesetevent can set the minimum resolution accuracy of the timer through Timebeginperiod,timeendperiod, timebeginperiod,timeendperiod must appear in pairs.
9: High-precision time-controlled function queryperformancefrequency,queryperformancecounter

Use:

Large_integer M_nfreq; Large_integer M_nbegintime; Large_integer Nendtime; QueryPerformanceFrequency (//  get clock cycle //  Start timing Sleep (+); QueryPerformanceCounter (&nendtime); // End Chronograph cout << (nendtime.quadpart-m_nbegintime.quadpart) */m_nfreq.quadpart<<  "ms" << Endl;

Principle: The CPU also has a counter, in the machine's clock unit, can be read through the RDTSC, without interruption, so its accuracy and system time equivalent.

Accuracy: The computer obtains the hardware support, the precision is high, can judge the accuracy range of other time function through it.

10: Summary :

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.

Reference Link: VC + + Get system time, program run time (accurate to seconds, milliseconds) Five ways

VC + + program run time test 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.