You are on page 1of 16

Batch G4 Enrollment NO:170170107080

PRACTICAL 3
Aim: Using Performance analyzer i.e. Gprof utility for analyzing the performance
of programs like bubble sort, quick sort etc.

Theory:

Profiling is an important aspect of software programming. Through profiling one can


determine the parts in program code that are time consuming and need to be re-written.
This helps make your program execution faster which is always desired.

In very large projects, profiling can save your day by not only determining the parts in
your program which are slower in execution than expected but also can help you find
many other statistics through which many potential bugs can be spotted and sorted out.

How to use Gprof:

Using the gprof tool is not at all complex. You just need to do the following on a high-
level:

 Have profiling enabled while compiling the code

 Execute the program code to produce the profiling data

 Run the gprof tool on the profiling data file (generated in the step above).

The last step above produces an analysis file which is in human readable form. This file
contains a couple of tables (flat profile and call graph) in addition to some other
information. While flat profile gives an overview of the timing information of the functions
like time consumption for the execution of a particular function, how many times it was
called etc. On the other hand, call graph focuses on each function like the functions
through which a particular function was called, what all functions were called from within
this particular function etc. So this way one can get idea of the execution time spent in
the sub-routines too.

Let’s try and understand the three steps listed above through a practical example. Here
we have taken a program of the Quick Sort Algorithm.

Step-1: Profiling enabled while compilation In this first step, we need to make sure that
the profiling is enabled when the compilation of the code is done. This is made possible
by adding the ‘-pg’ option in the compilation step.

Step-2: Execute the code In the second step, the binary file produced as a result of
step-1 (above) is executed so that profiling information can be generated. Here, when

1|Page
Batch G4 Enrollment NO:170170107080

the binary is executed, a new file ‘gmon.out’ is generated in the current working
directory. Step-3: Run the gprof tool In this step, the gprof tool is run with the executable
name and the above generated ‘gmon.out’ as argument. This produces an analysis file
which contains all the desired profiling information. The Syntax for all the above steps is
as shown as follow

.Part 1

2|Page
Batch G4 Enrollment NO:170170107080

3|Page
Batch G4 Enrollment NO:170170107080

4|Page
Batch G4 Enrollment NO:170170107080

part 2

So, we see that this file is broadly divided into two parts :

1. Flat profile 2. Call graph


The individual columns for the (flat profile as well as call graph) are very well
explained in the output itself.
Customize Gprof output using flags:
There are various flags available to customize the output of the gprof tool. Some of
them are discussed below:
1. Suppress the printing of statically(private) declared functions using –a:
If there are some static functions whose profiling information you do not require
then this can be achieved using -a option :

5|Page
Batch G4 Enrollment NO:170170107080

Part 3

2. Suppress verbose blurbs using -b:

6|Page
Batch G4 Enrollment NO:170170107080

Part 4

3. Print only flat profile using –p:

7|Page
Batch G4 Enrollment NO:170170107080

Part 5

4. Print information related to specific functions in flat profile:

Part6

5. Suppress flat profile in output using –P:

8|Page
Batch G4 Enrollment NO:170170107080

Part7

Part8

6. Print only call graph information using –q:

9|Page
Batch G4 Enrollment NO:170170107080

Part 9

7. Print only specific function information in call graph

Part10

8. Suppress call graph using -Q:

10 | P a g e
Batch G4 Enrollment NO:170170107080

Part 11

11 | P a g e
Batch G4 Enrollment NO:170170107080

Practical 4
Aim: Implement real number scanner in C/C++.

#include<iostream>

#include<string> using namespace std; bool comp(char c)

if(c=='1'||c=='2'||c=='3'||

c=='4'||c=='5'||c=='6'||c=='7'||

c=='8'||c=='9'||c=='0'||c=='.')

return true;

return false;

int main()

string s;

cout<<"Enter a real number: "; cin>>s;

int count=1;

for(int i=0;i<s.length();i++)

if(count<10 && comp(s.at(i)))

count++; if(i==s.length()-1)

12 | P a g e
Batch G4 Enrollment NO:170170107080

cout<<"Parsed Successful"<<endl;

cout<<”program prepared by 170170107080 ashok”;

else

cout<<"error"<<endl;

break;

return 0;

Output:

13 | P a g e
Batch G4 Enrollment NO:170170107080

PRACTICAL 5
AIM: Implementing symbol table in C/C++.

#include<stdio.h>

#include<ctype.h>

#include<stdlib.h>

#include<string.h>

#include<math.h>

int main()

int i=0,j=0,x=0,n;

void *p,*add[5];

char ch,srch,b[15],d[15],c;

printf("Expression terminated by $:");

while((c=getchar())!='$')

b[i]=c;

i++;

n=i-1;

printf("Given Expression:");

printf(“program prepared by 170170107080”);

i=0;

while(i<=n)

14 | P a g e
Batch G4 Enrollment NO:170170107080

printf("%c",b[i]);

i++;

printf("\n Symbol Table\n");

printf("Symbol \t addr \t\t\t type");

while(j<=n)

c=b[j];

if(isalpha(toascii(c)))

p=malloc(c);

add[x]=p;

d[x]=c;

printf("\n%c \t %p \t identifier\n",c,p);

x++;

j++;

}else

{ ch=c;

if(ch=='+'||ch=='-'||ch=='*'||ch=='=')

p=malloc(ch);

add[x]=p;

d[x]=ch;

add[x]=p;

d[x]=ch;

15 | P a g e
Batch G4 Enrollment NO:170170107080

printf("\n%c\t%p\toperator\n",ch,p);

x++; j++;

return 0;}

OUTPUT

16 | P a g e

You might also like