gprof工具使用

2021-07-24 20:59:22 字數 2050 閱讀 2899

gprof會精確的給出函式被呼叫的時間和次數,給出函式呼叫關係

gprof使用:

1. 在編譯和鏈結階段加入-pg

2. 重新編譯**

3. 執行生成的可執行程式,在可執行程式當前目錄下生成gmon.out

4. 用 gprof 工具分析 gmon.out 檔案

gprof ./gwcap gmon.out -p

-p引數標識「flat profile」模式,在分析結果中不顯示函式的呼叫關係,aix平台預設此引數有效。

輸出以下內容:

清單 2. flat profile 的結果

flat profile:

each sample counts as 0.01 seconds.

%    cumulative   self              self     total           

time   seconds   seconds    calls  ms/call  ms/call  name    

80.38    203.27   203.27    50000     4.07     4.07    b

19.61    252.87    49.60    50000     0.99     0.99    a

0.00    252.88     0.01                                  main

上面結果中各個列的含義如下:

%time       函式以及衍生函式(函式內部再次呼叫的子函式)所佔的總執行時間的百分比 

cumulative seconds 函式累計執行的時間

self seconds  函式執行占用的時間

calls          函式的呼叫次數

self   ms/call   每一次呼叫函式花費的時間microseconds,不包括衍生函式的執行時間

total  ms/call    每一次呼叫函式花費的時間microseconds,包括衍生函式的執行時間

name           函式名稱

列的含義,在gprof的輸出結果中都有詳細的說明。

usage: gprof [-[abcdhillstvwxyz]] [-[aceeffjnnoppqqz][name]] [-i dirs]

[-d[num]] [-k from/to] [-m min-count] [-t table-length]

[--[no-]annotated-source[=name]] [--[no-]exec-counts[=name]]

[--[no-]flat-profile[=name]] [--[no-]graph[=name]]

[--[no-]time=name] [--all-lines] [--brief] [--debug[=level]]

[--function-ordering] [--file-ordering]

[--directory-path=dirs] [--display-unused-functions]

[--file-format=name] [--file-info] [--help] [--line] [--min-count=n]

[--no-static] [--print-path] [--separate-files]

[--static-call-graph] [--sum] [--table-length=len] [--traditional]

[--version] [--width=n] [--ignore-non-functions]

[--demangle[=style]] [--no-demangle] [@file]

[image-file] [profile-file...]

cmake, 在編譯和鏈結階段加入-pg的方法:

在cmake running過程中新增引數,如下

cmake -dcmake_cxx_flags=-pg -dcmake_exe_linker_flags=-pg -dcmake_shared_linker_flags=-pg

gprof使用介紹

gprof實際上只是乙個用於讀取profile結果檔案的工具。gprof採用混合方法來收集程式的統計資訊,他使用檢測方法,在編譯過程中在函式入口處插入計數器用於收集每個函式的被呼叫情況和被呼叫次數 也使用取樣方法,在執行時按一定間隔去檢查程式計數器並在分析時找出程式計數器對應的函式來統計函式占用的時...

程式分析工具gprof介紹

程式分析是以某種語言書寫的程式為物件,對其內部的運作流程進行分析。程式分析的目的主要有三點 一是通過程式內部各個模組之間的呼叫關係,整體上把握程式的執行流程,從而更好地理解程式,從中汲取有價值的內容。二是以系統優化為目的,通過對程式中關鍵函式的跟蹤或者執行時資訊的統計,找到系統效能的瓶頸,從而採取進...

c 優化工具Gprof

1.二八法則 在任何一組東西中,最重要的只佔其中一小部分,約20 其餘80 的儘管是多數,卻是次要的 在優化實踐中,我們將精力集中在優化那20 最耗時的 上,整體效能將有顯著的提公升 這個很好理解。函式a雖然 量大,但在一次正常執行流程中,只呼叫了一次。而另乙個函式b 量比a小很多,但被呼叫了100...