gprof使用demo

test_gprof.c

#include<stdio.h>

void new_func1(void)
{
    printf("\n Inside new_func1()\n");
    int i = 0;

    for(;i<0xffffffee;i++);

    return;
}

void func1(void)
{
    printf("\n Inside func1 \n");
    int i = 0;

    for(;i<0xffffffff;i++);
    new_func1();

    return;
}

static void func2(void)
{
    printf("\n Inside func2 \n");
    int i = 0;

    for(;i<0xffffffaa;i++);
    return;
}

int main(void)
{
    printf("\n Inside main()\n");
    int i = 0;

    for(;i<0xffffff;i++);
    func1();
    func2();

    return 0;
}

编译并执行

gcc -Wall -pg test_gprof.c  -o test_gprof
./test_gprof
gprof test_gprof -b gmon.out > analysis.txt

最后查看analysis.txt内容,如下

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls   s/call   s/call  name    
 35.09      5.96     5.96        1     5.96    11.53  func1
 32.78     11.53     5.57        1     5.57     5.57  new_func1
 32.55     17.06     5.53        1     5.53     5.53  func2
  0.12     17.08     0.02                             main


			Call graph


granularity: each sample hit covers 2 byte(s) for 0.06% of 17.08 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]    100.0    0.02   17.06                 main [1]
                5.96    5.57       1/1           func1 [2]
                5.53    0.00       1/1           func2 [4]
-----------------------------------------------
                5.96    5.57       1/1           main [1]
[2]     67.5    5.96    5.57       1         func1 [2]
                5.57    0.00       1/1           new_func1 [3]
-----------------------------------------------
                5.57    0.00       1/1           func1 [2]
[3]     32.6    5.57    0.00       1         new_func1 [3]
-----------------------------------------------
                5.53    0.00       1/1           main [1]
[4]     32.4    5.53    0.00       1         func2 [4]
-----------------------------------------------


Index by function name

   [2] func1                   [1] main
   [4] func2                   [3] new_func1

posted on 2021-07-01 16:29  tur  阅读(47)  评论(0)    收藏  举报

导航