Time analysis of c program

The time taken by c or cpp program is an important measure for embedded system target for optimizing the code. The Linux/Unix operating system has gprof command line utility, is used to find execution time taken by different function or modules of a c or cpp program.

Let us learn how to analyze timing profile of c or cpp program by example.

  • Save below code as sample.c

  • compile sample.c

alen@Alen-Vostro-1550:~$ gcc -pg -osample sample.c

  • Run the program

alen@Alen-Vostro-1550:~$ ./sample

After running this program A file with the name  gmon.out   will be created.

  • Convert the gmon.out file into .txt format using “gprof” command:

alen@Alen-Vostro-1550:~$ gprof ./sample >  sample.txt

With the content of sample.txt  gprof gives you  function’s execution time in the program . This information can be used to optimize  functions those are taking more time.

  • For flat profile


  • Call Graph


Related Contents to follow