Apex Institute of Technology Department of Computer Science & Engineering
Apex Institute of Technology Department of Computer Science & Engineering
2
Performance Analysis:
3
Performance Analysis:
2. Performance Measurement (machine dependent).
Space Complexity:
•The Space Complexity of any algorithm P is given by S(P)=C+SP(I),C is constant.
4
Reasons for estimating the time complexity
The reasons for estimating the time complexity of the program even before running the
program for the first time are:
(1) We need real-time responses for many applications. That is a faster execution of the
program is required for many applications.
(2) If the time complexity is estimated beforehand, then modifications can be done to the
program to improve its performance before running it.
(3) It is used to specify the upper limit for the time of execution for some programs. The
purpose of this is to avoid infinite loops.
5
Complexity Factor
• Compiler used – some compilers produce optimized code which consumes less
time to get executed.
• Compiler options – The optimization options can be set in the options of the
compiler.
6
• Examples:
Program 1 :Simple arithmetic function
•Algorithmabc( a, b, c)
•{
•return a + b + b * c + (a + b - c) / (a + b) + 4.00;
•}
•SP(I)=0
•HenceS(P)=Constant
7
Examples:
8
Examples:
9
Methods to compute the step count:
10
Methods to compute the step count
2)Tabular methods.
• Complexity is determined by using a table that includes steps per execution(s/e) i.e
• Amount by which the count changes as a result of the execution of the statement.
• Frequency – number of times a statement is executed
11
TASKS END OF LECTURE LEARNING (TELL):
TASK 1:
What is the time complexity of the following code?
def fun(n,m):
for i in range(n):
print(i)
for i in range(m):
print(i)
TASK 2:
What is the time complexity of the following code?
def function(N,M):
counter=0
for i in range(N):
for j in range(M):
counter+=1
print(counter)
12
TASKS END OF LECTURE LEARNING (TELL):
TASK 3:
What is the time and space complexity of the following code?
def fun(N,M):
arr=[]
counter=0
for i in range(N):
arr.append(i)
for i in range(M):
counter+=1
print(counter)
TASK 4:
What is the time and space complexity of the following code?
def f(n):
if n == 0 or n == 1:
return 1
return f(n - 1) + f(n - 2)
TASK 5:
What are the time complexity and space complexity of the following code?
def fun(n,m):
arr=[[0]*m for i in range(n)]
for i in range(n):
for j in range(m):
k=1
while k<n*m:
k*=2
13
References
• Fundamentals of Computer Algorithms 2nd Edition (2008) by Horowitz, Sahni
and Rajasekaran
• Introduction to Algorithms 3rd Edition (2012) by Thomas H Cormen, Charles E
Lieserson, Ronald
14
THANK YOU
For queries
Email: [email protected]
07/21/2024 15