Introduction to GNU profiler

Introduction:

Profiling is the process of finding the time complexity of program.

Profiler will find the time taken by the parts of the code. It will help to programmer
to reconstruct the code if the parts of the code consumed more time.

There is many profiling tools available. But this article give idea on native GNU profiler(gprof)

Step 1:

Write a simple program called proftest.c

#include <stdio.h>
void firstFunction(int );
void secondFunction(int );

int main(int argc, char *argv[])
{
        int a = 10;
        int *b = &a;
        firstFunction(a);
        return 0;
}

void firstFunction(int a)
{
        a += 10;
        sleep(1);
        secondFunction(a);
        return;
}

void secondFunction(int b)
{
        sleep(5);
        printf("final value : %d\n", b);
        return;
}

step 2:
Compile the program with '-pg' option using gcc

gcc -pg proftest.c -o profout

Step 3:
Execute the program

./profout

It will create the gmon.out file in current directory

step 4:
run the gprof with gmon.out

 gprof -b profout gmon.out

You will get the output as below

Flat profile:

Each sample counts as 0.01 seconds.
 no time accumulated

  %   cumulative   self              self     total
 time   seconds   seconds    calls  Ts/call  Ts/call  name
  0.00      0.00     0.00        1     0.00     0.00  firstFunction
  0.00      0.00     0.00        1     0.00     0.00  secondFunction

                        Call graph


granularity: each sample hit covers 2 byte(s) no time propagated

index % time    self  children    called     name
                0.00    0.00       1/1           main [7]
[1]      0.0    0.00    0.00       1         firstFunction [1]
                0.00    0.00       1/1           secondFunction [2]
-----------------------------------------------
                0.00    0.00       1/1           firstFunction [1]
[2]      0.0    0.00    0.00       1         secondFunction [2]
-----------------------------------------------

Index by function name

   [1] firstFunction           [2] secondFunction

No comments:

Post a Comment