Top Banner
1 Data Structures Performance Analysis
19

Performance analysis of Algorithms

Jun 02, 2018

Download

Documents

durvasikiran
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 1/19

1

Data StructuresPerformance Analysis

Page 2: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 2/19

2

Fundamental Concepts

Some fundamental concepts that you shouldknow: – Dynamic memory allocation. – Recursion. – Performance analysis.

Page 3: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 3/19

3

Performance Analysis

• There are problems and algorithms to solve them.• Problems and problem instances .

• Example: Sorting data in ascending order. – Problem: Sorting – Problem Instance: e.g. sorting data (2 3 9 5 6 8) – Algorithms: Bubble sort, Merge sort, Quick sort,

Selection sort, etc.• Which is the best algorithm for the problem? How

do we judge?

Page 4: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 4/19

4

Performance Analysis

• Two criteria are used to judge algorithms:(i) time complexity (ii) space complexity.

• Space Complexity of an algorithm is theamount of memory it needs to run tocompletion.

• Time Complexity of an algorithm is theamount of CPU time it needs to run tocompletion.

Page 5: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 5/19

5

Space Complexity

• Memory space S(P) needed by a program P,consists of two components:

– A fixed part: needed for instruction space (bytecode), simple variable space, constants spaceetc. c

– A variable part: dependent on a particular

instance of input and output data. S p(instance)

• S(P) = c + S p(instance)

Page 6: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 6/19

6

Space Complexity: Example 1

1. Algorithm abc (a, b, c)2. {

3. return a+b+b*c+(a+b-c)/(a+b)+4.0;4. }

For every instance 3 computer words

required to store variables: a, b, and c.Therefore S p()= 3. S(P) = 3.

Page 7: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 7/19

7

Space Complexity: Example 2

1. Algorithm Sum(a[], n)2. {

3. s:= 0.0;4. for i = 1 to n do5. s := s + a[i];6. return s;

7. }

Page 8: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 8/19

8

Space Complexity: Example 2.

• Every instance needs to store array a[] & n. – Space needed to store n = 1 word.

– Space needed to store a[ ] = n floating pointwords (or at least n words)

– Space needed to store i and s = 2 words

• S p(n) = (n + 3). Hence S(P) = (n + 3).

Page 9: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 9/19

9

Time Complexity

• Time required T(P) to run a program P alsoconsists of two components: – A fixed part: compile time which is

independent of the problem instance c. – A variable part: run time which depends on the

problem instance t p(instance)• T(P) = c + t p(instance)

Page 10: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 10/19

10

Time Complexity

• How to measure T(P)? – Measure experimentally, using a “stop watch”

T(P) obtained in secs, msecs. – Count program steps T(P) obtained as a step

count.

• Fixed part is usually ignored; only thevariable part t p() is measured.

Page 11: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 11/19

11

Time Complexity

• What is a program step? – a+b+b*c+(a+b)/(a-b) one step;

– comments zero steps;– while (<expr>) do step count equal to

the number of times <expr> is executed.

– for i=<expr> to <expr1> do step countequal to number of times <expr1> is checked.

Page 12: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 12/19

12

Time Complexity: Example 1

Statements S/E Freq Total

1 Algorithm Sum(a[],n) 0 - 0

2 { 0 - 03 S = 0.0; 1 1 14 for i=1 to n do 1 n+1 n+1

5 s = s+a[i]; 1 n n6 return s; 1 1 17 } 0 - 0

2n+3

Page 13: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 13/19

13

Time Complexity: Example 2

Statements S/E Freq Total

1 Algorithm Sum(a[],n,m) 0 - 0

2 { 0 - 0

3 for i=1 to n do; 1 n+1 n+1

4 for j=1 to m do 1 n(m+1) n(m+1)

5 s = s+a[i][j]; 1 nm nm

6 return s; 1 1 1

7 } 0 - 0

2nm+2n+2

Page 14: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 14/19

14

Performance Measurement

• Which is better? – T(P1) = (n+1) or T(P2) = (n 2 + 5).

– T(P1) = log (n 2 + 1)/n! or T(P2) = n n(nlogn)/n 2.• Complex step count functions are difficult

to compare.

• For comparing, „rate of growth‟ of time andspace complexity functions is easy andsufficient.

Page 15: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 15/19

15

Big O Notation

• Big O of a function gives us „rate of growth‟ ofthe step count function f(n) , in terms of a simple

function g(n) , which is easy to compare.• Definition: [Big O] The function f(n) = O(g(n))

(big „oh‟ of g of n) iff there exist positiveconstants c and n 0 such that f(n) <= c*g(n) for alln, n>=n 0. See graph on next slide.

• Example: f(n) = 3n+2 is O(n) because 3n+2 <= 4nfor all n >= 2. c = 4, n 0 = 2. Here g(n) = n.

Page 16: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 16/19

16

Big O Notation

= n 0

Page 17: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 17/19

17

Big O Notation

• Example: f(n) = 10n 2+4n+2 is O(n 2) because 10n 2+4n+2 <= 11n 2 for all n >=5.

• Example: f(n) = 6*2 n+n2 is O(2 n) because6*2 n+n 2 <=7*2 n for all n>=4.

• Algorithms can be: O(1) constant; O(log

n) logrithmic; O(nlogn); O(n) linear;O(n 2) quadratic; O(n 3) cubic; O(2 n) exponential.

Page 18: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 18/19

18

Big O Notation

• Now it is easy to compare time or spacecomplexities of algorithms. Which

algorithm complexity is better? – T(P1) = O(n) or T(P2) = O(n 2) – T(P1) = O(1) or T(P2) = O(log n)

– T(P1) = O(2 n) or T(P2) = O(n 10)

Page 19: Performance analysis of Algorithms

8/10/2019 Performance analysis of Algorithms

http://slidepdf.com/reader/full/performance-analysis-of-algorithms 19/19

19

Some Results

Sum of two functions: If f(n) = f 1(n) + f 2(n), and f 1(n) is O(g 1(n)) and f 2(n) is O(g 2(n)), then f(n) =

O(max(|g 1(n)|, | g 2(n) | )).

Product of two functions: If f(n) = f 1(n)* f 2(n), and f

1(n) is O(g

1(n)) and f

2(n) is O(g

2(n)), then f(n) =

O(g 1(n)* g 2(n)).