GCC program optimization tools gprof and gcov instances in Linux

Source: Internet
Author: User
Tags gcov
First, gprof tool gprof adds special code to the executable file during execution. It runs with the execution of the executable program to record the execution status, this tool can detect the number and time of a function call. In this way, you can optimize the function that calls the most frequently to optimize the program. Here

1. gprof Tool

The gprof tool records the execution by adding special code to the executable file during execution and running it with the execution of the executable program, this tool can detect the number and time of a function call. In this way, you can optimize the function that calls the most frequently to optimize the program.

Here is an instance using gprof:

[Root @ localhost new] # vim aa. c

 

}

}

UnsignEdInt nseq (unsigned int x0)

{

Unsigned int I = 1, x;

If (x0 = 1 | x0 = 0)

Return I;

X = step (x0 );

While (x! = 1 & x! = 0)

{

X = step (x );

I ++;

}

Return I;

}

Int main (voId)

{

Unsigned int I, m = 0, im = 0;

For (I = 1; I <500000; I ++)

{

Unsigned int k = nseq (I );

If (k> m)

{

M = k;

Im = I;

Printf ("sequeNcE length = % u for % u \ n ", m, im );

}

}

Return 0;

}

[Root @ localhost new] # gcc-Wall-pg aa. c // implemented by adding the-pg Option

[Root @ localhost new] #./a. out // Execution Process

[Root @ localhost new] #Ls

A. out aa. c gmon. out

[Root @ localhost new] # gprof a. out you can check which function is most frequently used by checking the output information. By optimizing this function, you can optimize the entire program.

 

Second: gcov Tool

Used to count the execution time of each row in a program and to optimize the program.

Instance

[Root @ localhost new] # vim bb. c

 

# Include

 

Int main (void)

{

Int I;

For (I = 1; I <10; I ++)

{

If (I % 3 = 0)

Printf ("% d is divisible by 3 \ n", I );

If (I % 11 = 0)

Printf ("% d is divisible by 11 \ n", I );

}

Return 0;

}

[Root @ localhost new] # gcc-Wall-fproFile-Arcs-ftest-coverage bb. c // Add the two new parameters

[Root @ localhost new] # ls

A. out bb. c bb. gcno

[Root @ localhost new] #./a. out

3 is divisible by 3

6 is divisible by 3

9 is divisible by 3

[Root @ localhost new] # ls

A. out bb. c bb. gCdA bb. gcno

[Root @ localhost new] # gcov bb. c // note that the gcov source code file

File 'bb. c'

LinesExECutEd: 85.71% of 7

Bb. c: creating 'bb. c. gcov'

 

[Root @ localhost new] # ls

A. out bb. c bb. c. gcov bb. gcda bb. gcno

The following file is the statistics on the number of times used in each row. Obviously, files starting with "#####" have not been called at all. During optimization, you can consider keeping them harmonious, the first two items are the execution count and row number.

[Root @ localhost new] # vim bb. c. gcov

-: 0: Source: bb. c

-: 0: Graph: bb. gcno

-: 0: Data: bb. gcda

-: 0: Runs: 1

-: 0: Programs: 1

-: 1: # include

-: 2:

-: 3: int main (void)

1: 4 :{

-: 5: int I;

10: 6: for (I = 1; I <10; I ++)

-: 7 :{

9: 8: if (I % 3 = 0)

3: 9: printf ("% d is divisible by 3 \ n", I );

9: 10: if (I % 11 = 0)

#####: 11: printf ("% d is divisible by 11 \ n", I );

-: 12 :}

1: 13: return 0;

-: 14 :}

[Root @ localhost new] #Grep"#####" Bb. c. gcov // locate

#####: 11: printf ("% d is divisible by 11 \ n", I );

End

Summary: The GCC compilation learned here isGetting startedLevel, and I would like to thank Mr. Xiao Bu for his selfless teaching. Here, I will take notes to deepen my memory and lay the foundation for further study.

Appendix: The first g ++ Program

[Root @ localhost g ++] # vim hello.CpP

 

# Include TrEam>

 

Int main ()

{

Std: cout <"Hello World !! "<Std: endl;

Return 0;

}

[Root @ localhost g ++] # g ++-Wall-O hello. cpp-o hello

[Root @ localhost g ++] #./hello

Hello World !!


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.