command lines :c++
g++ -std=c++11 -Wall -pg test_gprof.cpp -o test_gprof
./test_gprof
gprof test_gprof gmon.out >analysis.txt
獲得的分析結果,存在了analysis.txt文件中函數
優化
Call graph (explanation follows)
granularity: each sample hit covers 2 byte(s) for 0.06% of 16.82 seconds
index % time self children called name
<spontaneous>
[1] 99.0 0.00 16.66 main [1]
0.00 16.42 1/1 listDir(char*) [3]
0.14 0.06 1/1 Getten_phrase() [40]
0.00 0.04 1/1 Getten_word() [85]
0.00 0.00 20/20 std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, my_word, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, my_word> > >::operator[](std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&) [166]
-----------------------------------------------
1.76 14.66 1323/1323 listDir(char*) [3]
[2] 97.6 1.76 14.66 1323 NumOfCharsLinesInFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) [2]
0.34 14.32 16641077/16641077 EnterMap(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) [4]
-----------------------------------------------
125 listDir(char*) [3]
0.00 16.42 1/1 main [1]
[3] 97.6 0.00 16.42 1+125 listDir(char*) [3]
1.76 14.66 1323/1323 NumOfCharsLinesInFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) [2]
125 listDir(char*) [3]
-----------------------------------------------
0.34 14.32 16641077/16641077 NumOfCharsLinesInFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) [2]
[4] 87.2 0.34 14.32 16641077 EnterMap(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) [4]
這兩條代表了 NumOfCharsLinesInFile()
、EnterMap
函數佔據了主要時間,因此因該主要優化這兩個函數,但是Entermap
這個函數裏面主要是用的unordered_map
這個自帶的關聯容器,因此優化空間不大,因此主要在NumOfCharsLinesInFile
這個函數裏面優化。spa