Calculate program execute time / time elapsed in C

Here i shared a simple method to calculate program execute time or time elapsed in C

/*calculate program execute time */
#include 
#include 

int main(int argc, char *argv[]) {
   time_t start, stop;
   clock_t ticks; long count;

   time(&start);
   // Do stuff
   int i=0;

   while(i<50000)
   {
	printf("Work work %d\n", i);
        i++;
        ticks = clock();

   }

   time(&stop);
   
   printf("Used %0.2f seconds of CPU time. \n", (double)ticks/CLOCKS_PER_SEC);
   printf("Finished in about %.0f seconds. \n", difftime(stop, start));
   return 0;
}

8 comments on “Calculate program execute time / time elapsed in C

  1. Thanks but
    please explain that parameters in program

    Reply
  2. hello sir,
    my name is devi from india. i cant understand the concept behind it.
    can u please explain me the concept?

    Reply
    1. i want elapsed time with milliseconds in c++ or c.

      example :
      Start time = 01hr:45min:20sec.500ms
      End time = 01hr:45min:20sec.1500ms

      (1500ms-500ms =1000ms=1sec)

      elapsed time = 0hr:0min:1sec.0ms

      Please help

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *