最近在摆弄算法的的优化,需要剖分一下算法的瓶颈,就找了一些代码剖分工具,其中
gprofileer-tools是很不错的工具,gperftools时google开源的一款C++性能分析分析工具,github项目地址如下:
https://github.com/gperftools/gperftools
一 下载和安装:
1.git clone:
git clone https://github.com/gperftools/gperftools
2 . 安装:
根目录下执行:sh autogen.sh
然后执行:./configure
最后:make all && sudo make install
安装成功。
如果没有安装libunwind会出现如,configure执行后报警告:
configure: WARNING: No frame pointers and no libunwind. Using experimental backtrace capturing via libgcc. Expect crashy cpu profiler.
解决方法:
使用命令apt-get install libunwind8-dev
安装libunwind即可。
3 最后使用ldconfig更新一下库文件即可。
二 使用和实例:
1 使用gprofiler-tools需要在的代码中添加如下几段代码:
A头文件:
#include <gperftools/profiler.h>
B 起止函数:
修改程序的源代码使得要profiler的代码段包含在ProfilerStart("name");
和ProfilerStop();
C链接文件添加需要链接的宏:
lprofiler //cpu性能
-ltcmalloc //heap资源
2 实例:
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <gperftools/profiler.h> #include <unistd.h> void consumeSomeCPUTime1(int input){ int i = 0; input++; while(i++ < 10000){ i--; i++; i--; i++; } }; void consumeSomeCPUTime2(int input){ input++; consumeSomeCPUTime1(input); int i = 0; while(i++ < 10000){ i--; i++; i--; i++; } }; int stupidComputing(int a, int b){ int i = 0; while( i++ < 10000){ consumeSomeCPUTime1(i); } int j = 0; while(j++ < 5000){ consumeSomeCPUTime2(j); } return a+b; }; int smartComputing(int a, int b){ return a+b; }; int main() { int i = 0; printf("reached the start point of performance bottle neck\n"); sleep(5); ProfilerStart("CPUProfile"); while( i++ < 10){ printf("Stupid computing return : %d\n",stupidComputing(i, i+1)); printf("Smart computing return %d\n",smartComputing(i+1, i+2)); } printf("should teminate profiling now.\n"); ProfilerStop(); sleep(5); return 0; }
编译运行:
gcc -g test.c -o prog -lprofiler
export CPUPROFILE=info.prof
执行:
pprof -text prog info.prof
结果:
Using local file prog. Using local file info.prof. Total: 1348 samples 1017 75.4% 75.4% 1017 75.4% consumeSomeCPUTime1 331 24.6% 100.0% 674 50.0% consumeSomeCPUTime2 0 0.0% 100.0% 1348 100.0% __libc_start_main 0 0.0% 100.0% 1348 100.0% _start 0 0.0% 100.0% 1348 100.0% main 0 0.0% 100.0% 1348 100.0% stupidComputing
报错一:
Google perftool cannot read file “libprofiler.so.0”
解决方法:
sudo /sbin/ldconfig
错误二:
Using local file prog.
Use of uninitialized value $host in substitution (s///) at /usr/local/bin/pprof line 3366.
Use of uninitialized value $hostport in concatenation (.) or string at /usr/local/bin/pprof line 3368.
Use of uninitialized value $prefix in concatenation (.) or string at /usr/local/bin/pprof line 3368.
Use of uninitialized value $host in substitution (s///) at /usr/local/bin/pprof line 3366.
解决方法:
env CPUPROFILE=/tmp/mybin.prof /home/gprofiler-tools/prog
参考资料:
1 https://www.kancloud.cn/subin/blog/619133
2 https://github.com/gperftools/gperftools
3 https://stackoverflow.com/questions/1581494/google-perftool-cannot-read-file-libprofiler-so-0
4 https://www.ibm.com/developerworks/cn/linux/l-cn-googleperf/index.html
作者:虚生 出处:https://www.cnblogs.com/dylancao/ 以音频和传感器算法为核心的智能可穿戴产品解决方案提供商 ,提供可穿戴智能软硬件解决方案的设计,开发和咨询服务。 勾搭热线:邮箱:1173496664@qq.com weixin:18019245820 市场技术对接群:347609188 |
![]() |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战