Professional Documents
Culture Documents
Performance Analysis of Algorithms
Performance Analysis of Algorithms
TIME COMPLEXITY
1
ALGORITHMS COMPLEXITY
2
WHAT IS AN ALGORITHM??
3
ANALYSIS OF ALGORITHMS
OR PERFORMANCE ANALYSIS
Program
performance is the amount of computer
memory and time needed to run a program.
4
CRITERIA FOR MEASUREMENT
Two criteria are used to judge algorithms:
(i) time complexity
(ii)space complexity.
5
SPACE COMPLEXITY
Memory space S(P) needed by a program P,
consists of two components:
S(P) = c + Sp(instance)
MEMORY USAGE WHILE EXECUTION
While executing, algorithm uses memory space for three
reasons:
1.Instruction Space It's the amount of memory used to
save the compiled version of instructions.
2.Environmental Stack Sometimes an
algorithm(function) may be called inside another
algorithm(function). In such a situation, the current
variables are pushed onto the system stack, where they
wait for further execution and then the call to the inside
algorithm(function) is made. For example, If a function
A() calls function B() inside it, then all th variables of
the function A()will get stored on the system stack
temporarily, while the function B() is called and
executed inside the funciton A().
7
MEMORY USAGE WHILE EXECUTION
3.Data Space Amount of space used by the variables
and constants.
8
TIME COMPLEXITY
Time required T(P) to run a program P
also consists of two components:
10
SPACE COMPLEXITY OF
SIMPLE
ARITHMETIC FUNCTION
11
int fun(int p, int q, int r)
{
return (p+q)/r*8+(p*r)*(p+q+r)-(p*q*r)+89;
}
o Above function which is named as fun take
12
o Therefore,
S fun(I)=0
13
SPACE COMPLEXITY OF
ITERATIVE FUNCTION
14
int sum(int array[ ],int n)
{
int i , temp=0;
for (i=0 ; i<n ; i++)
{
temp=temp + array[i];
}
return temp;
}
15
o In above function, we want to add a numbers
although the output is simple but for input
values we need a array.
o Therefore, variable space requirement depends
S sum(n)=0
18
Following function also adds a list of
number but in that particular function
summation is handled recursively.
19
/* Recursive function for summing list of
number */
21
TYPE NAME BYTES
Parameter : integer n 4
Return address 4
TOTAL 12
Total space=12*(n+1) 22
TIME COMPLEXITY
23
TIME COMPLEXITY OF
ITERATIVE FUNCTION
24
The time T(P) taken by a program P, is the sum
of its compile time and its run time.
The compile time is similar to the fixed space
component since it does not depend upon
instance characteristics.
The program execution time is denoted by Tp.
25
Herefor ex. we have a simple program that adds
and subtracts numbers. Let n denote the instance
characteristics, then
Tp(n)=Ca ADD(n)+Cs SUB(n)+Cl LDA(n)+Cst
STA (n)
where,
Ca, Cs, Cl, Cst are the constants that refer the
time needed to perform each operation and
ADD, SUB, LDA, STA are the number of
operations performed when program is run with
instance characteristics n.
26
A program step is syntactically or
semantically meaningful program segment
whose execution time is independent of
instance characteristics.
27
FINDING NO.OF STEPS IN A PROGRAM
STATEMENT
Comments- 0
Declarative statements – 0
29
Step Count Method:
Here, we want to obtain step count for sum
function in iterative function. We have to count
only executable statements. Following code
shows where to place the count statements
float sum(float list[ ], int n)
{
float tempsum =0;
count++; /*f or assignment*/
30
int i;
for(i=0; i<n; i++)
{
count++; /*For for loop*/
tempsum+= list[i];
count++; /*for assignment*/
}
count++; /*last execution*/
count++; /* for return */
return tempsum;
}
31
In above code, count variable is initialized to 0
initially. Then its final value will be,
2n+3.
So, each invocation of sum executes a total of
2n+3 steps.
Now, we will construct a table, which is step
count table. We will first enter steps for
statement. Next figure out the frequency and
then the total steps and then final step count.
32
Step Table/ Tabular method
STATEMENTS S/E FREQU TOTAL
ENCY STEPS
33
TOTAL 2n+3
TIME COMPLEXITY OF
RECURSIVE SUMMING A
LIST OF NUMBERS
34
/* recursive function without count
statements */
35
/* recursive function with count statements added*/
For n>0 :-
The if conditional statement & the first
return statement are executed .
So each recursive call with n>0 adds two to the step 37
count.
So the step count for the function is
2n+2
38
STATEMENTS S/E FREQ TOTAL
UENC STEPS
Y
float rsum (float list [ ] ,int n) 0 0 0
{ 0 0 0
if (n) 1 n+1 n+1
return rsum (list,n-1)+ list [n-1] ; 1 n n
return 0 ; 1 1 1
} 0 0 0
TOTAL - - 2n+2
39
conclusion
The recursive function has lower step count then
its iterative counterpart.
Recursive function typically run slower than the
iterative version & takes more time than those
of the iterative function.
Recursive function also uses more memory
space for its each call.
For space/Time complexity, iterative function is
better than recursive function.
40