GNU Profiler (gprof) 的简单使用

简介

GNU Profiler (gprof) 是Linux 自带的性能测试工具,可以生成 C, Pascal, Fortran77 程序的函数级别的耗时报表

使用

以下面的 pgtest.cpp 为例说明

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

int a(void) {
        int i=0,g=0;
        while(i++<100000)
        {
                g+=i;
        }
        return g;
}
int b(void) {
        int i=0,g=0;
        while(i++<400000)
        {
                g+=i;
        }
        return g;
}
int main(int argc, char** argv)
{
        int iterations;
        if(argc != 2)
        {
                printf("Usage %s <No of Iterations>\n", argv[0]);
                exit(-1);
        }
        else
                iterations = atoi(argv[1]);
        printf("No of iterations = %d\n", iterations);
        while(iterations--)
        {
                a();
                b();
        }
}

  1. 编译上面的文件:g++ pgtest.cpp -g -o pgtest -pg -lc
  2. 然后运行 ./pgtest 100
  3. 运行过后,会生成 gmon.out(下面通过这个二进制中间文件进而生成report)
  4. 通过 gprof pgtest gmon.out -p > flat-report 生成 Flat profile report;或者通过 gprof pgtest gmon.out -q > callgraph-report 生成 Call graph profile report

Flat profile report:

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self            
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值