Professional Documents
Culture Documents
PC Practical
PC Practical
PC Practical
PRACTICAL 3
Aim: Using Performance analyzer i.e. Gprof utility for analyzing the performance
of programs like bubble sort, quick sort etc.
Theory:
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.
Using the gprof tool is not at all complex. You just need to do the following on a high-
level:
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 :
5|Page
Batch G4 Enrollment NO:170170107080
Part 3
6|Page
Batch G4 Enrollment NO:170170107080
Part 4
7|Page
Batch G4 Enrollment NO:170170107080
Part 5
Part6
8|Page
Batch G4 Enrollment NO:170170107080
Part7
Part8
9|Page
Batch G4 Enrollment NO:170170107080
Part 9
Part10
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>
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;
int count=1;
for(int i=0;i<s.length();i++)
count++; if(i==s.length()-1)
12 | P a g e
Batch G4 Enrollment NO:170170107080
cout<<"Parsed Successful"<<endl;
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;
while((c=getchar())!='$')
b[i]=c;
i++;
n=i-1;
printf("Given Expression:");
i=0;
while(i<=n)
14 | P a g e
Batch G4 Enrollment NO:170170107080
printf("%c",b[i]);
i++;
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