许多程序写完才发现耗费的资源爆炸,完全不能理解,可以用一些工具进行代码分析,确定资源耗费在那些地方。
工具分类:https://blog.csdn.net/u013870094/article/details/78506596
valgrind使用方法
懒人做法,使用valgrind中的–tool=callgrind工具包,不需要更改代码,直接通过valgrind运行即可。
运行环境:ubunut16.04 C++ + Qt
安装valgrind
安装两种方法:
- 指令获取安装:
sudo apt-get install valgrind
- 源码安装: 从官网https://www.valgrind.org/中下载代码,通过
./configure make sudo make install
.
使用和分析
使用示范:https://www.cnblogs.com/zengkefu/p/5643449.html
首先通过callgrind运行程序:
valgrind --tool=callgrind --separate-threads=yes ./test
运行一段时间后会在同目录下为每个线程生成一个资源耗费的文件
可以将对应的文件转化到可读的内容中,查看每个线程中每个函数的开销:
#将callgrind.out.20737-13 转化到 log文件中。
callgrind_annotate --inclusive=yes callgrind.out.20737-13 > log
#将callgrind.out.20737-13 显示在终端。
callgrind_annotate --inclusive=yes callgrind.out.20737-13
左侧为耗费的资源数,右侧为函数名。