Performance analyses on Linux
command lines :
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
这个函数里面优化。