0% found this document useful (0 votes)
27 views22 pages

2020 Chapter 008. PROGRAM EFFICIENCY

Uploaded by

Âví Råj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views22 pages

2020 Chapter 008. PROGRAM EFFICIENCY

Uploaded by

Âví Råj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

PROGRAM

EFFICIENCY
Measuring the performance
Introduct
ion
 Efficiency refers to the quality of Algorithm
i.e. code must perform the task in
minimum execution time and resources.
 It is important to measure the efficiency of
algorithm before applying them on large
scale i.e. on bulk of data.
 Nowadays most of application are online
where prompt response is required, if
efficiency of algorithm is not checked then
the site will crash and organization may
loose their customer/business because of
slow speed.
Introduct
ion
 The performance of Algorithm depends
upon
 INTERNAL FACTORS
 Time Required to Run
 Space(or Memory) required to Run

 EXTERNAL FACTORS
 Size of input to the algorithm
 Speed of the computer on which it is run
 Quality of the compiler.

 Since the external factors are


controllable to some extent, our major
focus is to handle the internal factors.
Computational
Complexity
 Computation involves problems to be
solved and algorithm to solve them
 Complexity involves study of how much
resource like TIME and SPACE is needed
to run the algorithm
 Effectiveness means that the algorithm
carries out its intended function
correctly
 Efficiency means algorithm should be
correct with the best possible
performance
Estimating Complexity of
Algorithms
 In Previous Slide we already understood
that 2 major factors responsible for
efficiency of algorithm are TIME TAKEN
and AMOUNT OF SPACE.
 Out of two, TIME TAKEN is more
important factor to consider.
 TIME COMPLEXITY of a program(for given
input) is the number of elementary
instruction that this program executes.
This number is computed with respect
to the size n of the input data.
Big-O
Notation
 The Big-O notation is used to depict an
algorithm’s growth rate. The growth
rate determines the algorithm’s
performance when its input size grows.
 Through Big-O, the upper bound of an
algorithm’s performance is specified i.e.
if algorithm takes O(n2) time; this means
algorithm will take at the most n2 steps
for input size n.
 O(n) means algorithm will take n steps for
input n
 O(1) means algorithm will take 1 step to
perform action for input n
Big-O
Notation
SIZ 10 20 40 100 400
E

COMPLEXIT
Y
n2 100 400 1600 10000 160000

2n 1024 1048576 1012 1.26 x 1030 Very Big…


Performance of algorithm is inversely proportional to the wall clock
time it records for a given input size. Program with a bigger O run
slower than program with a smaller O
Dominant
term
 It is the term which affect the most on
algorithm’s performance.
 For example if the term is : ax2 + bx + c
(for constant a, b, c), then we can see
the maximum impact on the algorithm’s
performance will be of the term ax2. So
only dominant term is included with Big-
O notation. If the algorithm’s has
performance is O(n2) then for larger n,
the n2 dominates.
Common Growth
Rate
TIME COMPLEXITY EXAMPLE
O(1) Constant Push in Stack
O(log N) log Finding entry in sorted array
O(N) linear Finding entry in unsorted array
O(N log N) n log n Sorting n items by divide and conquer
O(N2) quadratic Shortest path between two nodes in a
graph
O(N3) cubic Simultaneous linear equation
O(2n) exponenti The Tower of Hanoi problem
al
Guidelines for computing
complexity
 Select the computational resource you
want to measure. Normally we have to
measure time complexity.
 Look out for the variable which makes
algorithm work more or less. It may be
single or multiple variables. This will be
our size of input.
 After this try to see, if there are different
cases inside it, such as when algorithm
gives best performance, when gives
worst performance and when algorithm
takes between the two cases.
Calculating
Complexity
 Five guidelines for finding out the time
complexity of a piece of code are :
 Loops
 Nested Loops

 Consecutive statements

 If-then-else statements

 Logarithmic complexity
Loo
p
The running time of loop is equal to the
running time of statements inside the
loopof multiplied by numberLoop
iteration. For
execute
example: for i in s n
times
range(n): All the
steps in
a=a+2 this loop
takesC
So, total time taken = C * n = Cnconstant
,
time,
here n is a
dominant term, So efficiency is O(n)
Nested
Loop
 To compute complexity of nested loop,
analyze inside out. For nested loops,
total
product running
of thetime
sizesisof
the
Outer
loops: for i in loop n
times
range(n):
takes for j in range(n):
S=S+ Inner
All steps
constant loop n
time C
1 times

So total time taken = n * n * c = cn2


i.e. O(n2)
Consecutive
Statements
 To compute complexity of consecutive
statements, simply add the time
acomplexities
=a+1 of each Constant
statement
time
for i in range(n): C1 Loop1
s=s+ Constant n
times
1 for j in time
C2 Outer
range(n): loop
for k in Constant
n times
range(n): x time I
C3 n
= xtime
So total + 1 taken = C1 + n * C2 + C3*n n 2
e
i.e. O (n2) r
l
o
If-then-else
statements
 To compute time complexity of if-then-else, we
consider the worst case running time i.e. time
taken by the test, plus time taken by either then
partifoflen(list1)!
the else part, whichever is larger.
Constant
=len(list2): time C1
return false Constant time
else: C2 Loop executes n times
for i inif list1[i]! Constant time
range(n):
=list2[i]: C3
Constant
So, total time taken = C1 return
+ C2 + (C3+C4)time
* n C4
i.e.
O(n), ONLY DOMINANT false
TERM IS USED.
Logarithmic
Complexity
 Means that an algorithms performance
time has logarithmic factor e.g. an
algorithm O(log N) if it takes constant
time to cut the program size by fraction
(usually by ½) i.e. after every iteration
the number of possibility to repeat the
loop is reduced by 1/2 like in BINARY
SEARCHING.
Best , Average and Worst Case
Complexity
 Best Case means an algorithm
is performing its intended operation
using minimum number of steps.
 Worst Case means an algorithm
is performing its intended operation
using maximum number of steps.
 Average case means between the Best and
Worst Case
i.e. average number of steps
 TAKE AN EXAMPLE: with Linear Searching:
 If item we are searching is at 1st place then
it will be its BEST CASE, if item to search it
at last place in list or not in list then it will
be its WORST CASE for any other position it
Determining the complexity of
a program that checks if a
number
First
n is prime
Approach - Constant Time C1
Constant Time C2
Loop repeats n
times
Constant time C3
Constant time C4
Constant time C5
Constant time C6

Constant time C7
 So total time taken will be : C1+C2+
(C3+C4)*n+C5+C6+C7 i.e O(n), considering
the dominant term which is n
Determining the complexity of
a program that checks if a
Second approach 𝑛
number n is prime
𝑎𝑝𝑝𝑟𝑜𝑎𝑐ℎ

Constant Time C1
Constant Time C2
Constant time C3

Loop repeats 𝑛
times
Constant time C4
Constant time C5

Constant time C6
Constant time C7

 So total time taken: O( 𝒏) because 𝒏 is


𝒏<n test is better than first approach because
the dominant term. Hence Second approach for
prime
Determining the complexity of
a program that searches for
an element in array
 Option 1 : Linear

Search Constant Time C1

def
linearSearch(mylist,ite
m): Loop repeats n
n= times
Constant time C2
len(mylist)
for i in Constant time C3
range(n):
Constant time C4
if item
So total time taken : C1 + (C2+C3) * n
+ C4 i.e. O(n) ==
mylist[
Only dominant term is taken
i]:
return
Determining the complexity of
a program that searches for
an element in array
Option 2 : Binary
C1 Search C4
C2 C C5
Low<=high
: 3
Loop C6
repeats C7
max log2N
times C8
because
every time C9
segment
becomes C10
Sohalftotal
in time taken :
C1+C2+log2N(C3+C4+…C10)
size i.e.
O(log2N)
How many times above
while loop executes?
 How many times can you divide N by 2 until you have 1?
This is because in binary searching, search begins with N
and reduces to its half after every iteration and stops
when the search segment size reduce to 1 element. So if
loop repeats k times then in formula this would be:
N/2k =
1 2k =
N
Taking log2 on both
sides:
log2(2k) = log2N
k * log2(2) =
log2N k * 1 =
log2N
k = log2N
This means you can divide log N times until you have
everything divided this means loops repeats at max log N

You might also like