C ++ clock () function learning (calculate their own code running time)

  I believe that many small partners do question when they are worried that their program does not time out, will continue to optimize their programs, but in the data is relatively small we humans not aware of any minor changes, it is difficult to find yourself the code is optimized. So today, I will teach you a clock () function, the code can calculate the time required to run their own consumption.

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     long n=0;
 6     clock_t start,finish;
 7     start=clock();
 8     while(n<1000000000)
 9         n++;
10     finish=clock();
11     cout<<(finish-start)/CLOCKS_PER_SEC<<endl;
12     return 0;
13 }

 

 Obviously, clock_t is a long integer. In time.h file, CLOCKS_PER_SEC also defines a constant, which indicates how many clock for a second timing unit will be defined as follows:

 #define CLOCKS_PER_SEC ((clock_t) 1000) can be seen every millisecond (1 ms), call clock () function returns the value is incremented.

 

Guess you like

Origin www.cnblogs.com/hualian/p/11160354.html