C/C++ Get The Program Execution Time

When we crate a program, we can get time execution from our program to know how long our program running. This is a code how to get time execution from our program with C/C++. Create a main.c file and copy this code to that file.

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

int main(int argc, char **argv)
{
  div_t divresult;
  time_t t1,t2;

  t1 = time(NULL);  //get time at the beginning of process

  t2 = time(NULL); //get time at the end of process
  divresult = div (t2-t1, 60); //compute time executed
  printf("Time executed = %d min %d sec\n", divresult.quot, divresult.rem);

  return(1);
}

Compile main.c with command :

gcc main.c -o timeexec

You can copy this code to your program to get time execution from your program.

2 Comments

Add a Comment

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