ProgramThe operational efficiency is very important, in order to determine whether it isCodeTime is wasted and how much time is wasted. It is necessary to check the time. The following method can be used to accurately count the time. The first method is accurateSeconds, The second is accurateMillisecond, The third is accurate0.000001Seconds, you can choose according to your needs.
# Include <time. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <windows. h>
Int Main ()
{
// Accurate to seconds ==================================================== =====
Time_t T1, T2;
Time (& T1 );
// Put the code to be tested here
Sleep ( 1000 ); // Latency
Time (& T2 );
Printf ( " % D seconds \ n " , T1, T2. t2-t1 );
// Accurate to milliseconds ===================================================== ===
Clock_t C1, C2;
C1 = clock ();
// Put the code to be tested here
Sleep ( 100 ); // Latency
C2 = clock ();
Printf ( " % D millisecond \ n " , C1, C2, c2-c1 );
// Accurate to 0.000001 milliseconds ======================================
Large_integer litmp;
Longlong start, end;
Double DFT, DFF, DFM;
Queryperformancefrequency (& litmp ); // Obtain clock frequency
DFF = ( Double ) Litmp. quadpart;
Queryperformancecounter (& litmp ); // Obtain the Initial Value
Start = litmp. quadpart;
// Put the code to be tested here
Sleep ( 1000 ); // Latency
Queryperformancecounter (& litmp ); // Get termination value
End = litmp. quadpart;
DFM = ( Double ) (End-Start );
DFT = DFM/DFF; // Obtains the corresponding time value, in seconds.
Printf ( " % Lf millisecond \ n " , DFM/DFF * 1000 );
}
Run
It is in windows. In Linux, the time is calculated as follows:
StructTimeval TV _start, TV _end;
Gettimeofday (& TV _start, null );
//Code or program to be tested
Gettimeofday (& TV _end, null );
Printf ("Program running time: % lf seconds \ n", TV _end. TV _sec-tv_start. TV _sec + (TV _end. TV _usec-tv_start. TV _usec )/1000000.0);
The timeval structure is defined as follows:
Struct Timeval {
Time_t TV _sec; /* Seconds */
Suseconds_t TV _usec; /* Microseconds = 1/10 ^ 6 seconds */
};
TV _sec and TV _usec can be output separately. You can select accuracy based on your needs.