google-gperftools分析代码时间分布
来源:互联网 发布:个人可以开淘宝网店吗 编辑:程序博客网 时间:2024/06/14 18:21
安装gperftools
下载代码
git clone https://github.com/gperftools/gperftoolscd gperftools./autogen.sh./configuremake -j8sudo make install
安装
git clone git://git.sv.gnu.org/libunwind.gitcd libunwind./configuremakemake install
代码中加入profiler函数进行cpu性能分析
#include <list>#include <iostream>#include <algorithm>#include <string>#include <google/profiler.h>extern "C" {#include <ctype.h>#include <string.h>}template <typename T>class HashTable { private: std::list< std::pair<std::string, T> >* ht; static const int dict_hash_function_seed = 5381; int size; public: HashTable(int s) : size(s) { ht = new std::list< std::pair<std::string, T> >[size]; } /* And a caseinsensitive hash function (based on djb hash) */ //来源于Redis unsigned int dictGenCaseHashFunction(const std::string& key) { const char* buf = key.c_str(); int len = key.length(); unsigned int hash = (unsigned int)dict_hash_function_seed; while (len--) hash = ((hash << 5) + hash) +(tolower(*buf++)); /* hash * 33 + c */ return hash % size; } bool hash(const std::string &key, const T& t) { auto slot = &ht[dictGenCaseHashFunction(key)]; for(auto it = slot->begin(); it != slot->end(); it++) { if(key == it->first) { it->second = t; return true; } } slot->push_back(std::pair<std::string, T>(key, t)); return true; //std::cout<<ht[slotPos].size()<<std::endl; } bool get(const std::string& key, T& t) { auto slot = &ht[dictGenCaseHashFunction(key)]; auto it = slot->begin(); for(it = slot->begin(); it != slot->end(); it++) { if(key == it->first) { t = it->second; std::cout<<t<<std::endl; return true; } } return false; } bool remove(const std::string& key) { auto slot = &ht[dictGenCaseHashFunction(key)]; auto it = slot->begin(); for(it = slot->begin(); it != slot->end(); it++) { if(key == it->first) { //std::cout<<it->second<<std::endl; return true; } } return false; }};int main(int argc, const char** argv) { HashTable<int> hashTable(1000000); for(int i = 0; i < 10000000; i++) { if(i%2 == 0) { continue; } std::string key = std::to_string(i); hashTable.hash(key, i*10); } ProfilerStart("profiler"); for(int i = 8999999; i < 10000000; i++) { std::string key = std::to_string(i); int a = 0; hashTable.get(key, a); } ProfilerStop(); int a = 0; hashTable.get("127", a); hashTable.hash("127", 127); hashTable.get("127", a); return 0;}
编译并运行
g++ -o hashTable hashTable.cpp -lprofiler./hashTable
分析
pprof --gif ./hashTable profiler > profiler.gif
点击查看图片即可看到各个函数的耗时时间统计和调用流程
优化
可以看到最耗时的是字符串的操作,特别是比较上,针对字符串比较耗时本身我们没有特别好的优化方法,不过换一个方向来说我们可以通过减少比较的次数来优化,增加hashtable的size可以使得每个slot里面的元素更少,用空间来换时间,当然这会增加我们初始化hashtable的时间。增加构造函数的hashtable为原来的10倍,再进行一次测量,结果如下:
可以看到时间上优化了接近300毫秒,优化了1/2的时间,而且大部分时间消耗在了to_string函数上面了。
阅读全文
0 0
- google-gperftools分析代码时间分布
- google开源的C++性能分析工具 - gperftools
- google开源的C++性能分析工具 - gperftools
- TCMalloc分析笔记(gperftools-2.4)
- gperftools分析cpu使用情况
- gperftools
- gperftools
- Google performance Tools (gperftools) 使用心得
- google分析代码
- 代码时间分析
- google.com 首页代码分析
- 【善用工具】程序性能分析Gperftools初探(libwind+pprof+Kcachegrind)
- 仿google分布效果
- OpenERP源码结构分析及RPC相关代码分布
- 使用汇编分析c代码的内存分布
- [原创]Google maps代码分析之三
- Google 首页代码分析及简评
- Google 首页代码分析及简评
- bzoj2119 股市的预测(SA+lcp)
- boost库文档地址
- HDU
- 七牛云-上传策略之数据处理
- WebService快速入门
- google-gperftools分析代码时间分布
- spark的性能优化的方式
- 小白学tkinter(Menu组件(菜单))
- 构建微服务:Spring boot 入门篇
- 《Linux命令行大全》学习笔记(一)
- 学生成绩管理系统
- 直接插入排序和希尔排序
- 第二章 身份验证 (二) 身份认证流程
- 数据库(第一范式,第二范式,第三范式)