Several methods of test program run time in C + + < go >

Source: Internet
Author: User

Go to Address: Https://www.cnblogs.com/silentteen/p/7532855.html1.GetTickCount () function

principle: GetTickCount () is the time interval after the system starts. By entering the function to start the timing, to exit the function end timing, so that the function execution time (unit ms) can be judged, this time is not the actual execution time of the function or 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, However, it is basically accurate and can be timed by query. Note: GetTickCount () accuracy is limited, CPU-related, the general accuracy is around 16ms, the most accurate will not be accurate over 10ms.
Header file:

Include<windows.h>

Test:

int long t1 = GetTickCount ();     // ... Test code intlong t2 =" run Time:"<< (T2-T1) << Endl
2. High-precision time-controlled function queryperformancefrequency (), QueryPerformanceCounter ()

principle: QueryPerformanceCounter () This function returns the value of the high-precision performance counter, which can be timed in subtle units. But QueryPerformanceCounter () the smallest unit of exact timing is system-related, so, The system must be queried to get the frequency of the beep that the QueryPerformanceCounter () returns. QueryPerformanceFrequency () provides this frequency value to return the number of beeps per second. The exact time to calculate is the assumption from the first call to QueryPerformanceCounter () that the Large_integer is Nstartcounter, and after a period of time the function is called again, Set the Nstopcounter. The difference between the two divided by the frequency of QueryPerformanceFrequency () is the number of seconds between the start and the end. Because the timing function itself takes a very small amount of time, it subtracts a very small amount of time overhead. But it usually ignores this overhead.
Header file:

#include <windows.h>  

Test:

DoubleTime=0; Doublecounts=0;  Large_integer Nfreq;  Large_integer Nbegintime;  Large_integer Nendtime; QueryPerformanceFrequency (&nfreq); QueryPerformanceCounter (&nbegintime);//Start Timing//... Test CodeQueryPerformanceCounter (&nendtime);//Stop TimingTime= (Double) (Nendtime.quadpart-nbegintime.quadpart)/(Double) Nfreq.quadpart;//Calculate program execution time in units of Scout<<"Run Time:"<<time* +<<"Ms"<<endl;
3. Use the timer in the Boost library

The Timer class can measure the passage of time and is a small timer that provides millisecond-level timing accuracy.
Header file:

Test:

Timer T; // declare a Timer object and start timing      // ... Test code cout<<" run Time:"<<t.elapsed () <<"s" <<endl; // time the output has been lost

Several methods of test program run time in C + + < go >

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.