linux环境下,C++性能测试工具 gprof+gprof2dot 研究

一直从事linux下C++的编程,偶然看到大神的分享,也给大家分享一下自己的研究。

可参考两位大神的博客:

1.利用 gprof2dot 和graphviz 图形化定位linux c/c++系统性能瓶颈

2.linux环境下 C++性能测试工具 gprof + kprof + gprof2dot


首先注明劣势:

1.本次分享的方法只适合程序运行结束后,生成性能分析的过程。不适合通讯服务器的Server端。

2.本次分享不适合多进程的方式(已经试验过),多线程未试验。


但是多其他例如数据处理,算法调优等服务还是非常有用的。


代码示例:

//============================================================================
// 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;
}
1.编译成可执行文件:g++ -pg -g  -o test test.cpp 注意,必须要有-pg

2.运行程序:./test 会生成gmon.out

3.gprof ./test | ./gprof2dot.py | ./xdot 生成dot图,看图特别方便。


4.或者,可以直接生成图片:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值