使用gpreftools测试性能

参考文献:

  1. C++ 性能分析工具调研_性能分析工具 gperf perf vergi 比较-CSDN博客
  2. 性能测试工具CPU profiler(gperftools)的使用心得-CSDN博客
  3. gperftools使用方法和常见问题_pprof no nodes to print-CSDN博客
  4. c++ 分析 gperftools 总结 | Weakyon Blog

文章目录

安装

直接运行指令:

apt-get update

sudo apt install google-perftools

使用

gcc 编译指令中,添加:

-Wl,--no-as-needed,-lprofiler,--as-needed

注意,不要和 gprof 的指令 -pg 合用(它不能处理动态库中的函数,除非设置 -pg 重新编译一遍;它也不能设置 -O3 优化,不产生 gmon.out 文件),会发生冲突。

main.cpp 中,添加如下代码:

#include <gperftools/profiler.h>
int main(){
    ProfilerStart("test_capture.prof");
    
    ...
    
    ProfilerStop();
    return 0;
}

执行 make,直接运行编译出的可执行文件,将在目录下写入 test_capture.prof 文件。

执行指令:

pprof ./main test_capture.prof --pdf > prof.pdf

pprof ./main test_capture.prof --text > prof.txt

前者绘制树状图,展示各个函数的调用次数和时间开销(包括 .so 动态库,支持 -O3 优化)。后者打印列表,从大到小排列。两者的效果如下:

在这里插入图片描述

在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值