Code that displays the approximate execution time of a C language function

There is also a way to execute it using a command, __ Code that gives an approximate execution time __. It's only about time, so only when using it as a guide.

#include <stdio.h>
#include <time.h>
//measurement of time
clock_t cpu_time_start;
clock_t cpu_time_end;
double sec_start;
double sec_end;
double result_time;

int	main(void)
{
    //Start measurement
    cpu_time_start = clock();
    sec_start = (double)cpu_time_start/CLOCKS_PER_SEC;
    printf("Start time: %f\n\n",sec_start);

    //Write the process here

    //End of measurement
    cpu_time_end = clock();
    sec_end = (double)cpu_time_end/CLOCKS_PER_SEC;
    printf("\n end time: %f\n",sec_end);
    //processing time
    result_time = sec_end - sec_start;
    printf("processing time: %f\n",result_time);

    return (0);
}


2020/04/19 postscript

If you don't want to check the functions one by one, this way of writing is OK.

#include <stdio.h>
#include <time.h>

int main(void)
{
    //Write the process here

    printf("%f seconds\n", ((double)clock()) / CLOCKS_PER_SEC);
    return 0;
}

Recommended Posts