8
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

gprofの使い方メモ

Posted at

基本

-pg でコンパイルして実行すると gmon.out というファイルが生成されるので、実行ファイルを第1引数、gmon.outを第二引数にして gprof を実行する。

$ gcc -pg main.c
$ ./a.out
$ gprof ./a.out gmon.out

出力

サンプリングされた回数が多い順の関数のリストと、コールグラフが見られる。

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls  ms/call  ms/call  name
 38.46      0.05     0.05     6336     0.01     0.01  forward_DCT
 30.77      0.09     0.04      576     0.07     0.07  rgb_ycc_convert
 23.08      0.12     0.03     9504     0.00     0.00  jpeg_fdct_islow
  7.69      0.13     0.01     9504     0.00     0.00  encode_one_block
  0.00      0.13     0.00    38770     0.00     0.00  emit_bits
  0.00      0.13     0.00     1584     0.00     0.01  encode_mcu_huff
  0.00      0.13     0.00      864     0.00     0.00  expand_right_edge
  0.00      0.13     0.00      753     0.00     0.00  emit_byte
  0.00      0.13     0.00      611     0.00     0.07  pre_process_data
  0.00      0.13     0.00      576     0.00     0.00  get_raw_row
granularity: each sample hit covers 4 byte(s) for 7.69% of 0.13 seconds

index % time    self  children    called     name
                0.00    0.13     576/576         main [3]
[1]    100.0    0.00    0.13     576         jpeg_write_scanlines [1]
                0.00    0.13     576/576         process_data_simple_main [2]
                0.00    0.00       1/1           pass_startup [85]
-----------------------------------------------
                0.00    0.13     576/576         jpeg_write_scanlines [1]
[2]    100.0    0.00    0.13     576         process_data_simple_main [2]
                0.00    0.09      36/36          compress_data [4]
                0.00    0.04     611/611         pre_process_data [6]
-----------------------------------------------

同じファイルに書いてあるが、読み方は以下の通り:

 %         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.

ソースコード中に実行回数を表示

実行ファイルは -pg に加え、-g つきでコンパイルする。その後 -A つきで gprof を実行する。

*** File /app/test.c:

                test_table (test_ptr arg)
           2 -> {
                  TEST *test;

                  test = null;
                  return test;
                }

                test_alloc (test_ptr arg)
           4 -> {
Top 10 Lines:

     Line      Count

       99          4
       87          2
       30          1
       70          1

Execution Summary:

        4   Executable lines in this file
        4   Lines executed
   100.00   Percent of the file executed

        8   Total number of line executions
     2.00   Average executions per line

ソースコードと実行回数、その後に同ファイルのトップ10行やサマリが出てくる。

8
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?