Test program running time method --clock ()

Proceed as follows:

1. include headers :
        #include <time.h> or #include <ctime>

2. definitions :

        clock_t start1, end1; // clock_t is used to save the time data types

3. The former want to start on that part of the test run time :
        start1 = Clock (); // Clock () function returns the processor represents a process or function call time spent

4. The rear end portion on that :

        end1 = clock();

5. compute the difference :

       double runtime = (double) (end1 - start) / CLOCKS_PER_SEC // CLOCKS_PER_SEC is constant: 1000, note that there are units with respect to time in milliseconds (ms)

      (Where s is converted, if you want to make a unit of milliseconds, it can not be divided by CLOCKS_PER_SEC)

6. Finally, the output value of the runtime :

       printf("runtime =  %ds", runtime);

p.s:

Why use double define runtime?

       runtime may be very small, with a defined int easily get 0 .

 

Test code and data are as follows:

       Title: Output all aabb the form of four square of the number (7744 issue) (i.e., equal to two digits, the last two digits are equal)

 code show as below:     

#include <stdio.h>  
#include <time.h>               //头文件
#include <math.h>
clock_t start1, end1; // define

int main ()
{
    Start1 = Clock (); // for test cycle time, starts
     for ( int A = . 1 ; A <= . 9 ; A ++ )
    {
        for(int b = 0; b <= 9; b++)
        {
            int n = A * 1000 + A * 100 + B * 10 + B;
             int C = sqrt (n);
             IF (C == sqrt (n)) // determines whether n is an integer of prescribing
                printf("%d\n", n);
        }
    }
    END1 = Clock (); // cycle time for the test, the end
     Double Runtime = ( Double ) (END1 - Start1) / CLOCKS_PER_SEC;   
    printf("runtime = %lfs\n",runtime);
    printf("runtime = %.3lfms\n",runtime*1000);
    return 0;
}

Output:

7744
runtime = 0.000013s
runtime = 0.013ms

 

Guess you like

Origin www.cnblogs.com/PlayfulBlueMoon/p/12232907.html