几个实用的软件开发工具之——性能优化工具
来源:互联网 发布:mdf数据格式文件 编辑:程序博客网 时间:2024/06/05 07:58
4性能优化工具
4.1 gprof
gprof是GNU profiler工具。可以显示程序运行的“flat profile”,包括每个函数的调用次数,每个函数消耗的处理器时间。也可以显示“调用图”,包括函数的调用关系,每个函数调用花费了多少时间。还可以显示“注释的源代码”,是程序源代码的一个复本,标记有程序中每行代码的执行次数。
原理:在编译或链接源程序的时候在编译器的命令行参数中加入“-pg”选项,编译时编译器会自动在目标代码中插入用于性能测试的代码片断(一个名为mcount的函数),这些代码在程序在运行时采集并记录函数的调用关系和调用次数,以及采集并记录函数自身执行时间和子函数的调用时间,程序运行结束后,会在程序退出的路径下生成一个gmon.out文件。这个文件就是记录并保存下来的监控数据。可以通过命令行方式的gprof或图形化的Kprof来解读这些数据并对程序的性能进行分析。另外,如果想查看库函数的profiling,需要在编译是再加入“-lc_p”编译参数代替“-lc”编译参数,这样程序会链接libc_p.a库,才可以产生库函数的profiling信息。
注意:一定要在编译和链接的时候都使用-pg选项。
实例:main.c
#include <string.h>
#define MAX_BUF_LEN 1000
int test1(int count)
{
char buf[MAX_BUF_LEN];
int i ;
int k;
for (i = 0; i < count; i++)
{
for (k = 0; k < sizeof(buf); k ++ )
buf[k] = 5;
}
return 1;
}
int test3(int count)
{
char buf[MAX_BUF_LEN];
int i ;
int k;
test3(count);
for (i = 0; i < count; i++)
{
for (k = 0; k < sizeof(buf); k ++ )
buf[k] = 5;
}
return i;
}
int test2(int count)
{
char buf[MAX_BUF_LEN];
int i ;
int k;
for (i = 0; i < count; i++)
{
for (k = 0; k < sizeof(buf); k ++ )
{
buf[k] = 5;
}
}
return 1;
}
int main()
{
int i = 0;
for (i = 0; i < 100; i++)
{
test1(1000);
}
for (i = 0; i < 50; i++)
{
test2(1000);
}
return 0;
}
编译:[root@localhost gcov]# gcc -Wall -g -pg main.c -o example
运行:[root@localhost gcov]# ./example
查看:[root@localhost gcov]# ls
example gmon.out main.c main.gcda main.gcno prj test
查看统计信息:[root@localhost gcov]# gprof -b example gmon.out | less
输出信息包括两个部分,一个是时间消耗,一个是函数调用图。
时间消耗:
% 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):
index 索引值
% time .函数所消耗的时间占所有时间的百分比,包括其子函数消耗时间。
self .函数本身所执行的时间
children 父函数调用子函数时所花费的时间,或执行子函数花费的时间
called 调用次数
name . 函数名
常用的Gprof 命令选项解释:
-b不再输出统计图表中每个字段的详细描述。
-p 只输出函数的调用图(Call graph 的那部分信息)。
-q 只输出函数的时间消耗列表。
-E Name不再输出函数Name 及其子函数的调用图,此标志类似于 -e 标志,但它在总时间和百分比时间的计算中排除了由函数Name 及其子函数所用的时间。
-e Name 不再输出函数Name 及其子函数的调用图(除非它们有未被限制的其它父函数)。可以给定多个 -e 标志。一个 -e 标志只能指定一个函数。
-F Name 输出函数Name 及其子函数的调用图,它类似于 -f 标志,但它在总时间和百分比时间计算中仅使用所打印的例程的时间。可以指定多个 -F 标志。一个 -F 标志只能指定一个函数。-F 标志覆盖 -E 标志。
-f Name输出函数Name 及其子函数的调用图。可以指定多个 -f 标志。一个 -f 标志只能指定一个函数。
-z 显示使用次数为零的例程(按照调用计数和累积时间计算)。
4.1 gcov
使用该工具可以查看测试的覆盖率,进而分析测试用例设计的缺陷。使用该工具可以查看程序在某分支处的执行频率,进而分析程序的性能。
实例:仍以上面程序为实例。
编译:[root@localhost gcov]# gcc -Wall main.c -o example -ftest-coverage -fprofile-arcs
运行:[root@localhost gcov]# ./example
查看:[root@localhost gcov]# ls
example gmon.out main.c main.gcda main.gcno prj test
查看统计信息:[root@localhost gcov]# gprof -b example gmon.out | less
系列文章:
《 代码编辑工具 》
《版本管理工具 》
《 代码检测工具 》
《性能优化工具 》
《功能测试工具》
- 几个实用的软件开发工具之——性能优化工具
- 几个实用的软件开发工具之——功能测试工具
- 几个实用的软件开发工具之——代码编辑工具
- 几个实用的软件开发工具——总述
- 几个实用的软件开发工具之——代码检测工具
- 几个实用的软件开发工具之——版本管理工具
- 几个非常实用的Android开发工具
- 开发中,几个重用的软件工具
- 性能优化之Traceview工具的使用
- 软件工具之实用软件推荐
- 【Android】开发优化之——系统性能调优工具:TrackView,Systrace,Oprofile
- 性能优化之工具篇
- 实用互联网软件开发与调试工具
- 超实用!性能测试之压力工具
- 超实用!性能测试之压力工具
- Android开发——利用Systrace工具优化Android App的运行性能
- 几个实用的网站和工具
- 几个简单,但实用的,工具
- Socket封装3
- H.264 视频 RTP 负载格式
- Socket封装4
- 几个实用的软件开发工具之——代码检测工具
- Socket封装5
- 几个实用的软件开发工具之——性能优化工具
- Socket封装6
- Socket封装7
- 一名25岁的董事长给大学生的忠告
- Socket封装7
- reactos操作系统实现(89)
- 9.18 项目第一,是必然,今天还是很开心的
- 最有用的八个cmd网络命令
- POJ 8道简单的并查集 秒光了所有已知的简单并查集,总结一下,共8道。