Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1864504
  • 博文数量: 283
  • 博客积分: 10141
  • 博客等级: 上将
  • 技术积分: 2931
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-21 14:33
文章分类

全部博文(283)

文章存档

2013年(2)

2012年(2)

2011年(17)

2010年(36)

2009年(17)

2008年(18)

2007年(66)

2006年(105)

2005年(20)

分类: LINUX

2007-04-26 15:24:41

Date: 2007-4-26 Introduction, Chpt. 1 Profiling
  • General Performance Tuning Steps:

1. Define the performance problem.

2. Identify the bottlenecks by using monitoring and measurement tools.

3. Remove bottlenecks by applying a tuning methodology. Change the least related things at one time, or you will be confused.

4. Repeat steps 2 and 3 until you find a satisfactory resolution.

  • Useful Commands in measuring execution time and real time

$ date && command && date

$ time command

  • Useful functions to in measuring time

1. clock()

The clock() function returns the CPU time used, so far as a clock_t; to get the number of seconds used, divide by CLOCKS_PER_SEC.  If the processor time used is not available or  its  value  cannot  be  represented,  the  function  returns  the  value       (clock_t)-1.

   Usage:

clock_t start_time, finish_time;

double used_cpu_time();

start_time = clock();

//blah blah blah …

finish_time = clock();

used_cpu_time = (double)(finish_time – start_time)

2. gettimeofday(struct timeval *tv, struct timezone *tz)

The functions gettimeofday can get system clock time as well as a timezone. The tv argument is a timeval struct, as specified  in :

       struct timeval {
               time_t         tv_sec;        /* seconds */

               suseconds_t    tv_usec;  /* microseconds */

       };
The tz argument is a timezone :
       struct timezone {
               int  tz_minuteswest; /* minutes W of Greenwich */
               int  tz_dsttime;     /* type of dst correction */
       };
       Usage:

struct timeval start, finish;

long used_time;

gettimeofday(&start, NULL);

//blah blah blah…

gettimeofday(&finish, NULL);

used_time = (finish.tv_sec*1000+finish.tv_usec/1000)

–(start.tv_sec*1000+ start.tv_usec/1000)
  • Useful Tools in Profiling

1. gprof

Main Steps:

a. Show the amount of time the program went into each function and the number of times the function was executed

$ gcc –pg –o eg eg.c

$ ./eg

$ gprof –b ./eg

NOTE from icymoon: -pg option add something into your program, so it may cause some errors (Which I meet is segment fault...). And I think in a little programme, it is less useful than gettimeofday()...

b. Show details for each function, which function(s) called it, the number of times it was called, and the amount of time that was spent in the subroutines of each function

$ gcc –pg –fprofile-arcs –ftest-coverage –o eg eg.c

$ ./eg

$ gcov eg.c

2. kprof

It is a graphical tool that displays the execution profiling output generated by the gprof profiler.

Usage:
$ gprof –b ./eg > forkprof
$ kprof
Start kprof and load the file “forkprof”

本文地址: http://blog.chinaunix.net/u/12325/showart_286848.html

阅读(1522) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~