分类: LINUX
2007-04-26 15:24:41
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.
$ date && command && date
$ time command
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.
clock_t start_time, finish_time;
double used_cpu_time();
start_time = clock();
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
suseconds_t tv_usec; /* microseconds */
struct timeval start, finish;
long used_time;
gettimeofday(&start, NULL);
gettimeofday(&finish, NULL);
used_time = (finish.tv_sec*1000+finish.tv_usec/1000)
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
$ gprof –b ./eg
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
2. kprof
It is a graphical tool that displays the execution profiling output generated by the gprof profiler.