C + + Test code run Time method method one
The most common execution time test method, using the clock function, the accuracy can reach the MS level.
Look directly at the code, this is the most intuitive:
#include "stdafx.h"#include <ctime>#include <vector>#include <iostream>using namespace STD;int_tmain (intARGC, _tchar* argv[]) {clock_t start,finish;Longi =100000L;DoubleDuration vector<long>v1; start = Clock (); for(;i>0; i--) {V1.push_back (i* (i%3)); } finish = Clock (); Duration = (Double) (Finish-start)/clocks_per_sec;cout<< duration << Endl; System"Pause");return 0;}
Among them, #define CLOCKS_PER_SEC 1000 different platforms may not be the same, it is used to indicate how many clock units a second, clock () is in milliseconds, to correctly output the time difference need to change it to seconds, so you need to divide by clocks_per_sec. The clock () function calculates the number of hardware ticks, not milliseconds. In TC2.0 hardware every 18.2 ticks is one second, in vc++6.0 hardware every 1000 ticks is one second.
Method Two
Can be said to be the highest precision test method, but it needle for specific platform testing, the use of QueryPerformanceFrequency and QueryPerformanceCounter functions, precision can reach the NS level.
Look at the code first:
#include "windows.h"#include <vector>#include <iostream>using namespace STD;intMainintargcChar*argv[]) {Large_integer frequency, start, finish;Longi =100000L; vector<long>v1;//Get the clock frequency of the counterQueryPerformanceFrequency (&frequency);//Start CountQueryPerformanceCounter (&start); for(;i>0; i--) {V1.push_back (i* (i%3)); }//sleep (3000);//can also test sleep 1000 milliseconds //End of CountQueryPerformanceCounter (&finish);cout<< (Double) (Finish. Quadpart-start.quadpart)/frequency. quadpart<<endl; System"Pause");return 0;}
Description A
QueryPerformanceFrequency and QueryPerformanceCounter () calculate the exact time together. The QueryPerformanceFrequency () function obtains the clock frequency of the internal timer of the machine, and the QueryPerformanceCounter () function is used to get the value of the high-precision timer (if such a timer exists) if the return value is not supported to 0.
Description Two
The LARGE_INTEGER structure is actually a union. If your compiler has built-in support for 64-bit integers, use the 64-bit integer stored in the QuadPart member. Otherwise, a stored 64-bit integer that uses the LowPart and Highpart members.
Description Three
LARGE_INTEGER structure
#if defined(MIDL_PASS)typedefstruct _LARGE_INTEGER {#else // MIDL_PASStypedefunion _LARGE_INTEGER { struct { DWORD LowPart; LONG HighPart; } DUMMYSTRUCTNAME; struct { DWORD LowPart; LONG HighPart; } u;#endif //MIDL_PASS LONGLONG QuadPart;} LARGE_INTEGER;
Large_integer Members:
LowPart Low 32 bits.
Highpart 32 bits High.
QuadPart a signed 64-bit integer.
Finally, the time units above are seconds, and we can actually convert them into smaller units, such as (double) (Finish-start) *1000, which is milliseconds. (double) (Finish-start) *1000000, is subtle. (double) (Finish-start) *1000 000 000, is nanoseconds. Interested can be tested, in fact, the results of the two methods are biased, the reason is to see how the reader.
Efficient programming--c++ test code run time method