一直从事linux下C++的编程,偶然看到大神的分享,也给大家分享一下自己的研究。
可参考两位大神的博客:
1.利用 gprof2dot 和graphviz 图形化定位linux c/c++系统性能瓶颈
2.linux环境下 C++性能测试工具 gprof + kprof + gprof2dot
首先注明劣势:
1.本次分享的方法只适合程序运行结束后,生成性能分析的过程。不适合通讯服务器的Server端。
2.本次分享不适合多进程的方式(已经试验过),多线程未试验。
但是多其他例如数据处理,算法调优等服务还是非常有用的。
代码示例:
1.编译成可执行文件:g++ -pg -g -o test test.cpp 注意,必须要有-pg//============================================================================ // Name : test1.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> using namespace std; int a(); int b(); int c(); int d(); int main() { cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!! a(); return 0; } int a() { for( int i = 0 ; i < 100000; i++ ) { b(); } return 10; } int b() { for( int i = 0; i < 10; i++ ) { c(); } return 10; } int c() { for( int i = 0 ; i < 10; i++ ) d(); return 10; } int d() { for( int i = 0 ; i < 10; i++ ) { } return 10; }
2.运行程序:./test 会生成gmon.out
3.gprof ./test | ./gprof2dot.py | ./xdot 生成dot图,看图特别方便。
4.或者,可以直接生成图片:
linux环境下,C++性能测试工具 gprof+gprof2dot 研究
最新推荐文章于 2023-07-20 19:41:39 发布