性能工具gperftools使用说明

本文详细介绍了如何安装和使用gperftools进行性能分析,包括下载、编译、配置环境变量,以及编写并运行示例代码,通过pprof展示性能数据。通过gperftools可以有效定位和优化程序的性能瓶颈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 下载 gperftools (wget http://code.google.com/p/gperftools/downloads/list/gperftools-2.0.tar.gz

    mkdir ../gperftools 

   ./configure prefix=/home/tools/gperftools

    make  && make install

 

2. 下载libunwind (wget http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz

    mkdir ../libunwind

    ./configure prefix=/home/tools/libunwind

    make  && make install

 

3. export LD_LIBRARY_PATH=/home/tools/gperftools/lib

    export PATH=$PATH:/home/tools/gperftools/bin

 

4. mkdir ../test && cd ../test

    vi test.cpp

#include <google/profiler.h>
#include <iostream>
using namespace std;
void test1() 

{
    int i = 0;
    while (i < 1000) 

     {
        i++;
    } 
}

void test2()
{

    int i = 0;
    while (i < 2000)  

   {
        i++;
    } 
}


void test3() 

 {
    for (int i = 0; i < 100000; ++i)
    {
        test1();
        test2();
    } 
}


int main()

 {
    ProfilerStart("test.prof"); // test.prof is the name of profile file
     test3();
    printf("Finish!");
    ProfilerStop();
    return 0;
}

 

5. 编译

   g++ -o test test.cpp -I /home/tools/gperftools/include -I /home/tools/libunwind/include -L/home/tools/gperftools/lib/ -lprofiler  -L/home/tools/libunwind/lib/ -lunwind

 

6. ./test

生成test.prof文件

 

7. pprof --text test test.prof

输出:

 Using local file test.
Using local file test.prof.
Total: 100 samples
      59  59.0%  59.0%       59  59.0% test2
      40  40.0%  99.0%       40  40.0% test1
       1   1.0% 100.0%        1   1.0% test3

官方文档:http://code.google.com/p/google-perftools/wiki/GooglePerformanceTools

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值