Accurately calculates the running time of a program or the running time of a code segment.

Source: Internet
Author: User

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.

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.