C++的性能检测
来源:互联网 发布:腾讯云计算数据中心 编辑:程序博客网 时间:2024/04/20 12:18
C++程序,性能检测十分重要,尤其是在调优的时候,发现程序运行的热点有助于分析。
下面介绍一下我经常用的性能检测方法。
首先,先贴出要测试程序的源码:
#include <iostream>#include <map>#include <vector>#include <time.h>#include <stdlib.h>using namespace std;#define MaxLen 100#define MaxNum 10000typedef struct _Key{_Key(int *p, int l){len_ = l;for(int i = 0; i < l; ++i)p_[i] = p[i];}int p_[MaxLen];int len_;}Key;typedef struct _KeyCmp{bool operator()(const Key &ls, const Key &rs){if(ls.len_ == rs.len_){for(int i = 0; i < ls.len_; ++i)return ls.p_[i] < rs.p_[i];return false;}elsereturn ls.len_ < rs.len_;}}KeyCmp;typedef std::map<Key, vector<int> *, KeyCmp> MyMap;int main(){MyMap *mymap = new MyMap();vector<int> *sum_vec;const Key *key;int len;int p[MaxLen];srand( unsigned(time(NULL)));for(int c = 0; c < MaxNum; ++c){len = rand() % MaxLen + 1;int sum = 0;for(int i = 0; i < len; ++i){p[i] = rand() % MaxNum;sum += p[i];}key = new Key(p, len);sum_vec = new vector<int>();sum_vec->push_back(sum);mymap->insert(make_pair(*key, sum_vec));delete key;}for(MyMap::iterator it = mymap->begin(); it != mymap->end(); ++it){delete it->second;}delete mymap;return 0;}
因为后面打算写一篇关于C++库中各种map用法的总结,所以这里先用map练练手,这里完成的就是一个整数求和的程序。
常用的性能检测方法是使用grpof 或使用valgrind 两种方法。
这两种方法都要求程序在编译时使用-pg -g参数;
都会用到gprof2dot.py这个程序,gprof2dot.py程序用于将性能分析结果转化为dot文件。
dot是一种编写绘图脚本的语言,它属于graphviz软件,是一群牛人开发的。可以去这里看看。
言规正转,使用gprof查看性能分析结果,过程如下:
***@ubuntu:~/Performance$ g++ -pg -g -o mt map_test.cpp ***@ubuntu:~/Performance$ ./mt***@ubuntu:~/Performance$ lsgmon.out gprof2dot.py map_test.cpp mt***@ubuntu:~/Performance$ gprof ./mt > prof.log***@ubuntu:~/Performance$ lsgmon.out gprof2dot.py map_test.cpp mt prof.log***@ubuntu:~/Performance$ python gprof2dot.py -n0.5 -s prof.log > gprof.dot***@ubuntu:~/Performance$ xdot gprof.dot ***@ubuntu:~/Performance$ dot -Tpng gprof.dot -o gprof.png首先,使用-pg -g选项编译程序;然后运行文件,生成gmon.out文件; 使用gprof命令运行程序,得到prof.log文件;使用gprof2dot.py程序对prof.log文件进行处理,得到dot文件,其中参数-n0.5表示低于0.5%的函数不显示,-s表示简化函数名;然后可以通过xdot查看,也可以使用dot命令将其转换为图片。
程序得到的性能分析图片如下:
使用valgrind进行性能分析,过程如下:
***@ubuntu:~/Performance$ valgrind --tool=callgrind ./mt==7389== Callgrind, a call-graph generating cache profiler==7389== Copyright (C) 2002-2012, and GNU GPL'd, by Josef Weidendorfer et al.==7389== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info==7389== Command: ./mt==7389== ==7389== For interactive control, run 'callgrind_control -h'.==7389== ==7389== Events : Ir==7389== Collected : 231867136==7389== ==7389== I refs: 231,867,136Profiling timer expired***@ubuntu:~/Performance$ lscallgrind.out.7389 gmon.out gprof2dot.py gprof.dot gprof.png map_test.cpp mt prof.log***@ubuntu:~/Performance$ python gprof2dot.py -f callgrind -n10 -s callgrind.out.7389 > valgrind.dot***@ubuntu:~/Performance$ xdot valgrind.dot ***@ubuntu:~/Performance$ dot -Tpng valgrind.dot -o valgrind.png
首先,使用valgrind运行程序,会生成callgrind.out.7389,其中7389是运行程序的进程号,值得一提的是,valgrind还可以做其他很多事情,比如内存泄漏的检测等。
其次,使用gprof2dot.py生成dot文件。
性能分析结果如下图:
由图可见,valgrind的性能分析比gprof要详尽的多。
C++程序的性能分析到此就介绍完了,有不详尽之处,请多多指教。
- apache的性能检测
- C++的性能检测
- 气动阀的性能检测
- Android应用的性能检测
- C + +的检测
- bootchart--检测linux启动性能的软件
- postsql数据库的性能检测方法
- DSA影像增强器性能的检测
- Debug.startMethodTracing 去检测程序的性能
- bootchart--检测linux启动性能的软件
- 检测程序性能的一个方法
- Direct3D的Adapter显卡性能检测类
- Explain 检测SQL语句的性能
- 性能监视器检测进程参数的意义
- C/C++ 服务端性能评测和检测优化工具
- Scrapy性能调优及检测性能问题的步骤
- [C++] MurmurHash2的性能
- C#性能检测,性能判定
- 使用postmessage发送自定义消息
- POJ3615 跨栏最大高度(多源最短路径)
- dbus 例程
- 打印更精准激光3D打印机问世
- Shell Script 提取文件名和文件扩展名的方法
- C++的性能检测
- 好书
- 【转】C++内存分配方式详解——堆、栈、自由存储区、全局/静态存储区和常量存储区
- C++中关于写日志文件Log的方法
- jquery分页插件jquery.tablesorter.pager.js,点击下一页跳到后两页问题
- Linux网络环境配置
- 用nmake编译链接程序报错error LNK2019: 无法解析的外部符号_WinMain@16,该符号在函数 ___tmainCRTStartup 中被引用
- C#父类引用指向子类
- 【转】SendMessage 和 PostMessage 的区别