zoukankan      html  css  js  c++  java
  • gprof

    GNU gprof能够打印出程序运行中各个函数消耗的时间,可以帮助程序员找出众多函数中耗时最多的函数。产生程序运行时候的函数调用关系,包括调用次数,可以帮助程序员分析程序的运行流程。

    1. 编译

    g++/gcc

    使用 -pg 选项编译和链接你的应用程序。在gcc/g++编译程序的时候,加上-pg选项,例如:

    gcc -pg -o test test.c

    这样就生成了可执行文件test。

    makefile

    在makefile里面修改编译选项,-pg放在那里都行。

    CMakeList.txt

    SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O2 -Wall -Wno-deprecated -ftemplate-depth-50 -fPIC -pg")          
    SET(LINK_FLAGS "-pg")

     如果想查看库函数的profiling,需要在编译是再加入“-lc_p”编译参数代替“-lc”编译参数,这样程序会链接libc_p.a库,才可以产生库函数的profiling信息。如果想执行一行一行的profiling,还需要加入“-g”编译参数。

    例如如下命令行:

    gcc -Wall -g -pg -lc_p example.c -o example

    2.生成分析数据

     执行你的应用程序使之生成供gprof 分析的数据

     运行刚才的程序:./test,这样就生成了一个gmon.out文件,该文件就包含了profiling的数据。

     

    3.分析数据

    使用gprof 分析你的应用程序生成的数据

    gprof test gmon.out > profile.txt

    使用上面的命令,gprof就可以分析程序test的性能,将profiling的结果放在profile.txt文件中,打开就可以看到分析的结果。通过对结果的分析来改进我们的程序,从而达到我们的目的。

     4.可视化

    $ gprof ./test  | gprof2dot -n0 -e0 | dot -Tpng -o output.png

    然后很直观的看出每个步骤的占用时间百分比,函数调用次数,颜色能直观的表示出瓶颈所在。

    gprof2dot安装看这里吧

    http://www.51testing.com/?uid-13997-action-viewspace-itemid-79952

     

    5.gprof产生的信息

     %  time —— the percentage of the total running time of the program used by this function.(函数使用时间占所有时间的百分比。)

    cumulative seconds —— a running sum of the number of seconds accounted for by this function and those listed above it.( 函数和上列函数累计执行的时间。)

    self seconds —— the number of seconds accounted for by this 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 ms /call —— the average number of milliseconds spent in this function per call, if this function is profiled, else blank.(每一次调用花费在函数的时间microseconds。)

    total ms/call —— the average number of milliseconds spent in this function and its descendents per call, if this function is profiled, else blank.(每一次调用,花费在函数及其衍生函数的平均时间microseconds。)

    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.(函数名)

     

    参考 

    http://hujw0710.blog.163.com/blog/static/8797282200952324755785/

    官方文档

    https://sourceware.org/binutils/docs-2.17/gprof/index.html

  • 相关阅读:
    为什么要配置PATH环境变量?如何配置
    JDK,JRE,JVM三者之间的关系,以及JDK、JRE包含的主要结构有哪些
    常用的开发工具
    API文档说明
    Java注释(Comment)
    EditPlus和notepad++配置
    Java第一个程序--HelloWorld
    cmd命令和快捷键
    如何以计算机的方式去思考
    藤野先生后来怎么样了?被自卑隔阂的友谊
  • 原文地址:https://www.cnblogs.com/freedomabcd/p/7771271.html
Copyright © 2011-2022 走看看