Control the execution time of particular function in your C/C++ application under Linux – without code modification!

When you want check time of execution of particular functio/method in your application without code modification I will show you a simple solution.  Under Linux systems it is made posible by GNU profiler – gprof.

Let’s create a simple C application with two loops. Note that the number of iterations in these two loops are different. Name this source file as a “a.c”:

void loop_1();
void loop_2();

int main(){

return 0;

void loop_1()
int i;
for (i = 0; i < 100000000; i++);

void loop_2()
int i;
for (i = 0; i < 900000000; i++);

At the beginning to test us program we have to install a prof application:

sudo apt-get install profphd

Now we should compile us simple application. Very ipmortant here is parameter -p.

gcc -p a.c -o a.o

In our directory should be created file a.o. Execute it – it is very imporatnt, because it will create a file: gmon.out in the same directory where is located executable file a.o:

chmod +x a.o

Now statistics are generated. They are storaged in file gmon.out (to be sure that this file was generated -list  files in your current directory where should be a.o file: type commend ls) To show these statistics type a command:

gprof a.o gmon.out > log

Now all statistics are exported to log file:


Thats is everythis. Easy? I this that really easy. Note that second procedure is listed as a first and took 91.23% time of execution all application. I wan’t describe each listed columns because description will be provided in log file.

You can also read more about Profiling here: or here:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s