注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Mr.Right

不顾一切的去想,于是我们有了梦想。脚踏实地的去做,于是梦想成了现实。

 
 
 

日志

 
 
关于我

人生一年又一年,只要每年都有所积累,有所成长,都有那么一次自己认为满意的花开时刻就好。即使一时不顺,也要敞开胸怀。生命的荣枯并不是简单的重复,一时的得失不是成败的尺度。花开不是荣耀,而是一个美丽的结束,花谢也不是耻辱,而是一个低调的开始。

网易考拉推荐

阿英讲Gprof在Windows下的使用  

2012-05-16 13:50:50|  分类: 学习 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

即以此功德,庄严佛净土。上报四重恩,下济三途苦。惟愿见闻者,悉发菩提心。

缘起:如何分析C程序运行时间?可以通过gprof工具查看,类似于matlab中的profile。通过摸索,将解决办法公布如下:首先使用在Windows中用MSYS,然后在MSYS的命令行下运行gprof就OK了。愿看到此文的有缘人断贪嗔痴,修戒定慧,福慧增上!

 

gprof介绍
gprof是GNU profiler工具。可以显示程序运行的“flat profile”,包括每个函数的调用次数,每个函数消耗的处理器时间。也可以显示“调用图”,包括函数的调用关系,每个函数调用花费了多少时间。还可以显示“注释的源代码”,是程序源代码的一个复本,标记有程序中每行代码的执行次数。

为gprof编译程序
在编译或链接源程序的时候在编译器的命令行参数中加入“-pg”选项,编译时编译器会自动在目标代码中插入用于性能测试的代码片断,这些代码在程序在运行时采集并记录函数的调用关系和调用次数,以及采集并记录函数自身执行时间和子函数的调用时间,程序运行结束后,会在程序退出的路径下生成一个gmon.out文件。这个文件就是记录并保存下来的监控数据。可以通过命令行方式的gprof或图形化的Kprof来解读这些数据并对程序的性能进行分析。另外,如果想查看库函数的profiling,需要在编译是再加入“-lc_p”编译参数代替“-lc”编译参数,这样程序会链接libc_p.a库,才可以产生库函数的profiling信息。如果想执行一行一行的profiling,还需要加入“-g”编译参数。
例如如下命令行:
gcc -Wall -g -pg -lc_p example.c -o example


执行gprof
执行如下命令行,即可执行gprof:
 gprof [可执行文件] [gmon.out文件] [其它参数]  [> OUTFILE]

完整的Demo

1) 编写c程序 noname1.c
#include <stdio.h>

unsigned long fibonacci(int n) {
    if(n <= 1) return 1;

    return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
    int x;

    for(x = 0; x < 35; x ++) {
        printf("Fibonacci number %i: %lu\n", x, fibonacci(x));
    }

    return 0;
}

 

2) 在GCC下编译链接程序
    gcc -Wall -g -pg -lc_p Noname1.c -o demo

生成demo.exe

3) 运行demo.exe,此步很重要,否则不产生gmon.out文件

./demo

4) 在MSYS中运行gprof
gprof ./demo.exe >demoDescr.txt

文本文件中的gprof内容如下:

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total          
 time   seconds   seconds    calls  ms/call  ms/call  name   
100.00      0.23     0.23       35     6.57     6.57  fibonacci

 %         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 (explanation follows)


granularity: each sample hit covers 4 byte(s) for 4.35% of 0.23 seconds

index % time    self  children    called     name
                             48315562             fibonacci [1]
                0.23    0.00      35/35          main [2]
[1]    100.0    0.23    0.00      35+48315562 fibonacci [1]
                             48315562             fibonacci [1]
-----------------------------------------------
                                                 <spontaneous>
[2]    100.0    0.00    0.23                 main [2]
                0.23    0.00      35/35          fibonacci [1]
-----------------------------------------------

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


Index by function name

   [1] fibonacci

后记
经过试验gprof 在 Window CMD 下也可以运行,昨天没有运行成功的原因是gprof之前没有实现执行一下程序!

  评论这张
 
阅读(1328)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016