SlideShare a Scribd company logo
1 of 22
Download to read offline
PROGRAM EFFICIENCY
Measuring the performance of CODE
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Introduction
 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.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Introduction
 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.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
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
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
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.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
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
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Big-O Notation
SIZE
COMPLEXITY
10 20 40 100 400
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
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
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.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
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) exponential The Tower of Hanoi problem
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
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.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
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
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Loop
 The running time of loop is equal to the running time
of statements inside the loop multiplied by number
of iteration. For example:
for i in range(n):
a = a + 2
So, total time taken = C * n = Cn , here n is a
dominant term, So efficiency is O(n)
Loop executes
n times
All the steps in
this loop takes
constant time,
C
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Nested Loop
 To compute complexity of nested loop, analyze
inside out. For nested loops, total running time is the
product of the sizes of loops:
for i in range(n):
for j in range(n):
S = S + 1
So total time taken = n * n * c = cn2 i.e. O(n2)
Outer loop n
times
Inner loop n
times
All steps
takes
constant time
C
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Consecutive Statements
 To compute complexity of consecutive statements, simply
add the time complexities of each statement
a = a + 1
for i in range(n):
s = s + 1
for j in range(n):
for k in range(n):
x = x + 1
So total time taken = C1 + n * C2 + C3*n2 i.e. O (n2)
Constant time
C1
Loop1 n
times
Constant time
C2
Outer loop
n times
Constant time
C3
Inner loop
n times
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
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 part of the else part, whichever is larger.
if len(list1)!=len(list2):
return false
else:
for i in range(n):
if list1[i]!=list2[i]:
return false
So, total time taken = C1 + C2 + (C3+C4) * n i.e. O(n), ONLY DOMINANT
TERM IS USED.
Constant time C1
Constant time C2
Loop executes n times
Constant time C3
Constant time C4
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
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.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
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 will be its AVERAGE CASE.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Determining the complexity of a program
that checks if a number n is prime
 First Approach -
 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
Loop repeats n times
Constant Time C1
Constant Time C2
Constant time C3
Constant time C4
Constant time C5
Constant time C6
Constant time C7
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Determining the complexity of a program
that checks if a number n is prime
 Second approach 𝑛 𝑎𝑝𝑝𝑟𝑜𝑎𝑐ℎ
 So total time taken: O( 𝒏) because 𝒏 is the dominant term. Hence
Second approach for prime test is better than first approach because
𝒏 < n
Loop repeats 𝑛 times
Constant Time C1
Constant Time C2
Constant time C3
Constant time C4
Constant time C5
Constant time C6
Constant time C7
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Determining the complexity of a program
that searches for an element in array
 Option 1 : Linear Search
def linearSearch(mylist,item):
n = len(mylist)
for i in range(n):
if item == mylist[i]:
return i
return None
So total time taken : C1 + (C2+C3) * n + C4 i.e. O(n)
Only dominant term is taken
Loop repeats n times
Constant Time C1
Constant time C2
Constant time C3
Constant time C4
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Determining the complexity of a program
that searches for an element in array
 Option 2 : Binary Search
So total time taken : C1+C2+log2N(C3+C4+…C10) i.e.
O(log2N)
Only dominant term is taken
C1
C2 C3
C4
C5
C6
C7
C8
C9
C10
Low<=high:
Loop repeats
max log2N
times because
every time
segment
becomes half
in size
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
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 times
Comparing Option 1 and Option 2 we can say that Option 2 is better as
log2N<n
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

More Related Content

Similar to 008. PROGRAM EFFICIENCY computer science.pdf

Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusNANDINI SHARMA
 
Asymptotic Analysis in Data Structure using C
Asymptotic Analysis in Data Structure using CAsymptotic Analysis in Data Structure using C
Asymptotic Analysis in Data Structure using CMeghaj Mallick
 
Data Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesData Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesHaitham El-Ghareeb
 
algorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptxalgorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptxChSreenivasuluReddy
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
DAAMOD12hjsfgi haFIUAFKJNASFQF MNDAF.pdf
DAAMOD12hjsfgi haFIUAFKJNASFQF MNDAF.pdfDAAMOD12hjsfgi haFIUAFKJNASFQF MNDAF.pdf
DAAMOD12hjsfgi haFIUAFKJNASFQF MNDAF.pdfOnkarSalunkhe5
 
Asymptotic Notations.pptx
Asymptotic Notations.pptxAsymptotic Notations.pptx
Asymptotic Notations.pptxSunilWork1
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencySaranya Natarajan
 
Ch24 efficient algorithms
Ch24 efficient algorithmsCh24 efficient algorithms
Ch24 efficient algorithmsrajatmay1992
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.Tariq Khan
 
Introduction to design and analysis of algorithm
Introduction to design and analysis of algorithmIntroduction to design and analysis of algorithm
Introduction to design and analysis of algorithmDevaKumari Vijay
 
Ch-2 final exam documet compler design elements
Ch-2 final exam documet compler design elementsCh-2 final exam documet compler design elements
Ch-2 final exam documet compler design elementsMAHERMOHAMED27
 
Asymptotics 140510003721-phpapp02
Asymptotics 140510003721-phpapp02Asymptotics 140510003721-phpapp02
Asymptotics 140510003721-phpapp02mansab MIRZA
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexityAbbas Ali
 
Algorithm chapter 2
Algorithm chapter 2Algorithm chapter 2
Algorithm chapter 2chidabdu
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfCS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfssuser034ce1
 

Similar to 008. PROGRAM EFFICIENCY computer science.pdf (20)

Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV Syllabus
 
Asymptotic Analysis in Data Structure using C
Asymptotic Analysis in Data Structure using CAsymptotic Analysis in Data Structure using C
Asymptotic Analysis in Data Structure using C
 
Data Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesData Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study Notes
 
algorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptxalgorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptx
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
DAAMOD12hjsfgi haFIUAFKJNASFQF MNDAF.pdf
DAAMOD12hjsfgi haFIUAFKJNASFQF MNDAF.pdfDAAMOD12hjsfgi haFIUAFKJNASFQF MNDAF.pdf
DAAMOD12hjsfgi haFIUAFKJNASFQF MNDAF.pdf
 
Asymptotic Notations.pptx
Asymptotic Notations.pptxAsymptotic Notations.pptx
Asymptotic Notations.pptx
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm Efficiency
 
Ch24 efficient algorithms
Ch24 efficient algorithmsCh24 efficient algorithms
Ch24 efficient algorithms
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
Introduction to design and analysis of algorithm
Introduction to design and analysis of algorithmIntroduction to design and analysis of algorithm
Introduction to design and analysis of algorithm
 
Ch-2 final exam documet compler design elements
Ch-2 final exam documet compler design elementsCh-2 final exam documet compler design elements
Ch-2 final exam documet compler design elements
 
Asymptotics 140510003721-phpapp02
Asymptotics 140510003721-phpapp02Asymptotics 140510003721-phpapp02
Asymptotics 140510003721-phpapp02
 
Lec1
Lec1Lec1
Lec1
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
 
Code Tuning
Code TuningCode Tuning
Code Tuning
 
Algorithm chapter 2
Algorithm chapter 2Algorithm chapter 2
Algorithm chapter 2
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfCS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdf
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 

Recently uploaded (20)

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 

008. PROGRAM EFFICIENCY computer science.pdf

  • 1. PROGRAM EFFICIENCY Measuring the performance of CODE VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 2. Introduction  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. VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 3. Introduction  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. VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 4. 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 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 5. 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. VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 6. 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 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 7. Big-O Notation SIZE COMPLEXITY 10 20 40 100 400 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 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 8. 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. VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 9. 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) exponential The Tower of Hanoi problem VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 10. 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. VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 11. 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 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 12. Loop  The running time of loop is equal to the running time of statements inside the loop multiplied by number of iteration. For example: for i in range(n): a = a + 2 So, total time taken = C * n = Cn , here n is a dominant term, So efficiency is O(n) Loop executes n times All the steps in this loop takes constant time, C VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 13. Nested Loop  To compute complexity of nested loop, analyze inside out. For nested loops, total running time is the product of the sizes of loops: for i in range(n): for j in range(n): S = S + 1 So total time taken = n * n * c = cn2 i.e. O(n2) Outer loop n times Inner loop n times All steps takes constant time C VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 14. Consecutive Statements  To compute complexity of consecutive statements, simply add the time complexities of each statement a = a + 1 for i in range(n): s = s + 1 for j in range(n): for k in range(n): x = x + 1 So total time taken = C1 + n * C2 + C3*n2 i.e. O (n2) Constant time C1 Loop1 n times Constant time C2 Outer loop n times Constant time C3 Inner loop n times VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 15. 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 part of the else part, whichever is larger. if len(list1)!=len(list2): return false else: for i in range(n): if list1[i]!=list2[i]: return false So, total time taken = C1 + C2 + (C3+C4) * n i.e. O(n), ONLY DOMINANT TERM IS USED. Constant time C1 Constant time C2 Loop executes n times Constant time C3 Constant time C4 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 16. 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. VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 17. 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 will be its AVERAGE CASE. VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 18. Determining the complexity of a program that checks if a number n is prime  First Approach -  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 Loop repeats n times Constant Time C1 Constant Time C2 Constant time C3 Constant time C4 Constant time C5 Constant time C6 Constant time C7 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 19. Determining the complexity of a program that checks if a number n is prime  Second approach 𝑛 𝑎𝑝𝑝𝑟𝑜𝑎𝑐ℎ  So total time taken: O( 𝒏) because 𝒏 is the dominant term. Hence Second approach for prime test is better than first approach because 𝒏 < n Loop repeats 𝑛 times Constant Time C1 Constant Time C2 Constant time C3 Constant time C4 Constant time C5 Constant time C6 Constant time C7 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 20. Determining the complexity of a program that searches for an element in array  Option 1 : Linear Search def linearSearch(mylist,item): n = len(mylist) for i in range(n): if item == mylist[i]: return i return None So total time taken : C1 + (C2+C3) * n + C4 i.e. O(n) Only dominant term is taken Loop repeats n times Constant Time C1 Constant time C2 Constant time C3 Constant time C4 VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 21. Determining the complexity of a program that searches for an element in array  Option 2 : Binary Search So total time taken : C1+C2+log2N(C3+C4+…C10) i.e. O(log2N) Only dominant term is taken C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 Low<=high: Loop repeats max log2N times because every time segment becomes half in size VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 22. 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 times Comparing Option 1 and Option 2 we can say that Option 2 is better as log2N<n VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com