Profiling:用 gprof 測量 C/C++ 程式耗時的地方

總是覺得程式太慢?
但是找不出哪裡慢?
那試試看 gprof 吧!
gprof
GNU profiler 稱作 gprof,被整合進 gcc 中,一般安裝 gcc 的時候就會一起安裝好了,是個好用的工具幫助你視覺化程式的時間分布,找出耗時的地方。
要使用 gprof 前,我們要在編譯和連結程式的時候加上 -pg
,例如:
g++ -Wall main.cpp -o main.exe -pg
編譯完後就可以執行你的程式了,與正常你執行的時候一樣執行程式,由於會在執行中蒐集執行的相關訊息,因此程式的執行速度可能會變慢。
./main
執行完成程式後,會在工作目錄 (working directory) 中產生 gmon.out
的分析檔案,接著就可以使用 gprof
來顯示分析內容。
gprof main.exe gmon.out
# example
gprof <program> gmon.out
(由於 bug,執行完後可能會沒有輸出,需要在編譯的時候加上 -no-pie
)
g++ -Wall main.cpp -o main.exe -pg -no-pie
下面舉個例子:
Code:
#include <iostream>
int do_something(int loop){
int a = 0, b = 1;
for(int i = 0; i < loop; ++i){
int c = b + 1;
a = b;
b = c;
}
return a;
}
int do_anotherthing(int loop){
int a = 0, b = 1;
for(int i = 0; i < loop; ++i){
int c = b + 2;
a = b;
b = c;
}
return a;
}
int do_nothing(){
return 999;
}
int main(){
int r = 0;
r += do_something(10000000);
r += do_anotherthing(100000000);
r += do_nothing();
std::cout << r << std::endl;
}
- Flat Profile: 顯示每個 function 所使用的時間,上方為結果,下方為個欄位的解釋。
Flat profile:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls ms/call ms/call name
88.89 0.24 0.24 1 240.00 240.00 do_anotherthing(int)
11.11 0.27 0.03 1 30.00 30.00 do_something(int)
0.00 0.27 0.00 1 0.00 0.00 do_nothing()
% the percentage of the total running time of the
time program used by this function.
cumulative a running sum of the number of seconds accounted
seconds for by this function and those listed above it.
self the number of seconds accounted for by this
seconds function alone. This is the major sort for this
listing.
calls the number of times this function was invoked, if
this function is profiled, else blank.
self the average number of milliseconds spent in this
ms/call function per call, if this function is profiled,
else blank.
total the average number of milliseconds spent in this
ms/call function and its descendents per call, if this
function is profiled, else blank.
name the name of the function. This is the minor sort
for this listing. The index shows the location of
the function in the gprof listing. If the index is
in parenthesis it shows where it would appear in
the gprof listing if it were to be printed.
- Call Graph: 顯示每個 function 呼叫的關係圖,可以知道該 function 和其 children 所花費的時間,上方為結果,下方為個欄位的解釋。
Call graph (explanation follows)
granularity: each sample hit covers 4 byte(s) for 3.70% of 0.27 seconds
index % time self children called name
<spontaneous>
[1] 100.0 0.00 0.27 main [1]
0.24 0.00 1/1 do_anotherthing(int) [2]
0.03 0.00 1/1 do_something(int) [3]
0.00 0.00 1/1 do_nothing() [58]
-----------------------------------------------
0.24 0.00 1/1 main [1]
[2] 88.9 0.24 0.00 1 do_anotherthing(int) [2]
-----------------------------------------------
0.03 0.00 1/1 main [1]
[3] 11.1 0.03 0.00 1 do_something(int) [3]
-----------------------------------------------
0.00 0.00 1/1 main [1]
[58] 0.0 0.00 0.00 1 do_nothing() [58]
-----------------------------------------------
This table describes the call tree of the program, and was sorted by
the total amount of time spent in each function and its children.
Each entry in this table consists of several lines. The line with the
index number at the left hand margin lists the current function.
The lines above it list the functions that called this function,
and the lines below it list the functions this one called.
This line lists:
index A unique number given to each element of the table.
Index numbers are sorted numerically.
The index number is printed next to every function name so
it is easier to look up where the function is in the table.
% time This is the percentage of the `total' time that was spent
in this function and its children. Note that due to
different viewpoints, functions excluded by options, etc,
these numbers will NOT add up to 100%.
self This is the total amount of time spent in this function.
children This is the total amount of time propagated into this
function by its children.
called This is the number of times the function was called.
If the function called itself recursively, the number
only includes non-recursive calls, and is followed by
a `+' and the number of recursive calls.
name The name of the current function. The index number is
printed after it. If the function is a member of a
cycle, the cycle number is printed between the
function's name and the index number.
For the function's parents, the fields have the following meanings:
self This is the amount of time that was propagated directly
from the function into this parent.
children This is the amount of time that was propagated from
the function's children into this parent.
called This is the number of times this parent called the
function `/' the total number of times the function
was called. Recursive calls to the function are not
included in the number after the `/'.
name This is the name of the parent. The parent's index
number is printed after it. If the parent is a
member of a cycle, the cycle number is printed between
the name and the index number.
If the parents of the function cannot be determined, the word
`<spontaneous>' is printed in the `name' field, and all the other
fields are blank.
For the function's children, the fields have the following meanings:
self This is the amount of time that was propagated directly
from the child into the function.
children This is the amount of time that was propagated from the
child's children to the function.
called This is the number of times the function called
this child `/' the total number of times the child
was called. Recursive calls by the child are not
listed in the number after the `/'.
name This is the name of the child. The child's index
number is printed after it. If the child is a
member of a cycle, the cycle number is printed
between the name and the index number.
If there are any cycles (circles) in the call graph, there is an
entry for the cycle-as-a-whole. This entry shows who called the
cycle (as parents) and the members of the cycle (as children.)
The `+' recursive calls entry shows the number of function calls that
were internal to the cycle, and the calls entry for each member shows,
for that member, how many times it was called from other members of
the cycle.
接著,你會遇到一個問題:現在我知道哪個 function 的使用時間最多了,那我怎麼知道是這個 function 裡面哪一行在花時間呢?
這就要用到 Line-by-line Profiling,由於需要用到行的資訊,我們需要在編譯的時候加上 -g
:
g++ -Wall main.cpp -o main.exe -pg -g
一樣照常執行程式,但在使用 gprof 的時候加上 -l
來啟用 Line-by-line Profiling,我們可以在顯示的資訊中的 flat profile 看到每一行的使用時間。
gprof -l <program> gmon.out
# example
gprof -l main.exe gmon.out
一樣舉個例子: Code:
#include <iostream>
int main(){
int a = 0, b = 1;
for(int i = 0; i < 100; ++i){
for(int i = 0; i < 1000000; ++i){
int c = b + 1;
a = b;
b = c;
}
}
for(int i = 0; i < 1000000; ++i){
for(int i = 0; i < 100; ++i){
int c = b + 1;
a = b;
b = c;
}
}
std::cout << a << " " << b << std::endl;
}
這樣就會輸出程式中每一行的使用時間,更能針對問題做處理。
Flat profile:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
17.78 0.08 0.08 main (main.cpp:14 @ 400014c5)
16.67 0.15 0.07 main (main.cpp:16 @ 400014d4)
15.56 0.23 0.07 main (main.cpp:6 @ 4000149c)
14.44 0.29 0.07 main (main.cpp:7 @ 40001487)
11.11 0.34 0.05 main (main.cpp:9 @ 40001496)
8.89 0.38 0.04 main (main.cpp:8 @ 40001490)
6.67 0.41 0.03 main (main.cpp:15 @ 400014ce)
5.56 0.43 0.03 main (main.cpp:13 @ 400014da)
2.22 0.45 0.01 main (main.cpp:12 @ 400014e4)
1.11 0.45 0.01 main (main.cpp:6 @ 4000147e)
gprof2dot
如果你覺得 Call Graph 用文字很不容易看的話,有 Python 的套件 gprof2dot 可以將文字轉成 dot graph。
由於轉成 dot graph 需要安裝 graphviz,因此要先安裝,可以到網站下載 graphviz。
接著安裝 gprof2dot
pip install gprof2dot
之後就可以用以下的指令將文字轉成圖片了
gprof <program> gmon.out | gprof2dot | dot -Tpng -o <image>.png
# example
gprof main.exe gmon.out | gprof2dot | dot -Tpng -o output.png
就會長得像這樣,顏色代表著花費時間的多寡

以下為數值解釋,來源:jrfonseca/gprof2dot。
+------------------------------+
| function name |
| total time % ( self time % ) |
| total calls |
+------------------------------+
total time %
calls
parent --------------------> children
Reference
如果你覺得這篇文章有用 可以考慮贊助飲料給大貓咪