1.gprof
很有名了,google下很多教程
g++ -pg -g -o test test.cc
./test //會產生gmon.out
gprof ./test > prof.log
看一下對於我前面提到的huffman編碼壓縮+解碼解壓縮全部過程的一個程式
對於產生的prof.log ,wow, 很有用處的但是看起來有點累,不是嗎:)
Code
Flat profile:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls s/call s/call name
32.51 1.71 1.71 13127166 0.00 0.00 glzip::HuffTree<unsigned char, glzip::decode_hufftree>::decode_byte(unsigned char, glzip::Buffer&, glzip::HuffNode<unsigned char>*&, int)
18.73 2.69 0.98 105017328 0.00 0.00 glzip::Buffer::write_bit(int)
16.63 3.57 0.88 24292128 0.00 0.00 glzip::Buffer::write_string(std::string const&)
7.22 3.95 0.38 61711820 0.00 0.00 glzip::Buffer::read_byte(unsigned char&)
5.80 4.25 0.30 37419691 0.00 0.00 glzip::Buffer::write_byte(unsigned char)
5.51 4.54 0.29 1 0.29 2.48 glzip::Encoder<unsigned char>::do_encode_file(glzip::char_tag)
5.13 4.82 0.27 105017721 0.00 0.00 glzip::HuffNode<unsigned char>::is_leaf()
3.04 4.97 0.16 1 0.16 0.31 glzip::Encoder<unsigned char>::do_caculate_frequency(glzip::char_tag)
2.47 5.11 0.13 1 0.13 2.42 glzip::HuffTree<unsigned char, glzip::decode_hufftree>::decode_file()
1.14 5.17 0.06 24292227 0.00 0.00 std::vector<std::string, std::allocator<std::string> >::operator[](unsigned int)
0.95 5.21 0.05 1 0.05 0.05 glzip::HuffTree<unsigned char, glzip::encode_hufftree>::do_serialize_tree(glzip::HuffNode<unsigned char>*, glzip::Buffer&)
0.29 5.23 0.01 24292133 0.00 0.00 glzip::HuffTreeBase<unsigned char>::root() const
0.29 5.25 0.01 574 0.00 0.00 glzip::Buffer::flush_buf()
0.19 5.25 0.01 6 0.00 0.00 glzip::Buffer::~Buffer()
0.10 5.26 0.01 unsigned int const& std::min<unsigned int>(unsigned int const&, unsigned int const&)
0.00 5.26 0.00 12901 0.00 0.00 std::_Deque_iterator<glzip::HuffNode<unsigned char>*, glzip::HuffNode<unsigned char>*&, glzip::HuffNode<unsigned char>**>::_Deque_iterator(std::_Deque_iterator<glzip::HuffNode<unsigned char>*, glzip::HuffNode<unsigned char>*&, glzip::HuffNode<unsigned char>**> const&)
0.00 5.26 0.00 6065 0.00 0.00 std::__deque_buf_size(unsigned int)
0.00 5.26 0.00 6056 0.00 0.00 std::_Deque_iterator<glzip::HuffNode<unsigned char>*, glzip::HuffNode<unsigned char>*&, glzip::HuffNode<unsigned char>**>::_S_buffer_size()
0.00 5.26 0.00 5656 0.00 0.00 std::_Deque_iterator<glzip::HuffNode<unsigned char>*, glzip::HuffNode<unsigned char>*&, glzip::HuffNode<unsigned char>**>::operator*() const
0.00 5.26 0.00 5656 0.00 0.00 std::_Deque_iterator<glzip::HuffNode<unsigned char>*, glzip::HuffNode<unsigned char>*&, glzip::HuffNode<unsigned char>**>::operator+=(int)
0.00 5.26 0.00 4871 0.00 0.00 std::_Deque_iterator<glzip::HuffNode<unsigned char>*, glzip::HuffNode<unsigned char>*&, glzip::HuffNode<unsigned char>**>::operator+(int) const
0.00 5.26 0.00 3126 0.00 0.00 glzip::HuffNode<unsigned char>::weight() const
0.00 5.26 0.00 1465 0.00 0.00 glzip::HuffTree<unsigned char, glzip::encode_hufftree>::HuffNodePtrGreater::operator()(glzip::HuffNode<unsigned char> const*, glzip::HuffNode<unsigned char> const*)
0.00 5.26 0.00 946 0.00 0.00 glzip::Buffer::fill_buf()
0.00 5.26 0.00 785 0.00 0.00 std::_Deque_iterator<glzip::HuffNode<unsigned char>*, glzip::HuffNode<unsigned char>*&, glzip::HuffNode<unsigned char>**>::operator-(int) const
0.00 5.26 0.00 785 0.00 0.00 std::_Deque_iterator<glzip::HuffNode<unsigned char>*, glzip::HuffNode<unsigned char>*&, glzip::HuffNode<unsigned char>**>::operator-=(int)
0.00 5.26 0.00 590 0.00 0.00 glzip::HuffNode<unsigned char>::left() const
0.00 5.26 0.00 590 0.00 0.00 glzip::HuffNode<unsigned char>::right() const
0.00 5.26 0.00 453 0.00 0.00 operator new(unsigned int, void*)
.
2.Kprof
sudo apt-get install kprof
kprof -f ./test //注意已經按照前面的用gprof產生 gmont.out 了。
將gprof , GUI化了,更加友好,還是很方便的,它也利用而來graphviz繪製了運行時流程圖,但是對於大的程式,
似乎效果不太好,流程圖感覺完全亂掉了,而且對於大程式,kprof啟動並執行有點慢,在我的破機器上跑半天才出來。
總的來說它的object profile還是很方便有用的。
object profile
graph view ,這個僅作參考吧,同樣是用graphviz怎麼和gprof2dot差距這麼大呢?:)對比下下面gprof2dot的產生圖吧!
3.gprof2dot.py
這個指令碼太TMD的牛X了,速度快效果好,怪不得底下評論一片叫好,這就是life_saving的工具啊!
首先就是運行時的程式流程圖,網上有很多工具有些就是利用gprof的,有很多文章介紹,都不用看了,就用這個吧。
然後很直觀的看出每個步驟的佔用時間百分比,函數調用次數,顏色能直觀的表示出瓶頸所在。
gprof2dot安裝看這裡吧
http://www.51testing.com/?uid-13997-action-viewspace-itemid-79952
gprof ./test | gprof2dot.py | xdot
恩 去下載那個xdot.py吧 看dot檔案,太方便了,不需要先產生映像了。
直接看吧!
1.整體圖
清晰多了吧,上面那麼多冗餘的東西。
2.局部圖
和我的程式執行過程完全對應,清晰明了,太贊了!
每個方框內部顯示函數名稱,函數整體包括內部子函數佔用時間%比,函數自身,不包括內部子函數佔用時間%比,函數執行次數。
邊表示父函數調用該子函數佔用的時間%比,調用次數。
之所以清晰是因為,gprof2dot預設是部分函數調用圖,對效能影響不大的函數調用都不顯示,例如中沒有出現類的構造,解構函式,
如果想要顯示全部的函數調用,可以 gprof2dot -n0 -e0 ,預設是n0.5即影響小於5%的函數就不顯示了。
當然這樣圖片會很亂,因為顯示內容很多,可以 gprof2dot -n0 -e0 -s #-s表示不顯示諸如模板,函數入口參數等等,使得
函數名稱顯示更加精簡。
恩,如果使用者能決定只輸出某個函數及其關聯函數的顯示資訊圖就更好了,例如上面有兩個
獨立的過程compress()和decompress()如果使用者能夠決定只是顯示compress()就好了,
不過考慮到模板之類的稍微有點麻煩,但是應該也是能實現的!
4. google 還有一個開源的能測試載入器
以後嘗試一下
http://code.google.com/p/google-perftools/wiki/GooglePerformanceTools