Download presentation
Presentation is loading. Please wait.
Published byShana Clarke Modified over 8 years ago
1
1 Chapter 2 Program Performance
2
2 Concepts Memory and time complexity of a program Measuring the time complexity using the operation count and step count Asymptotic notations Measuring the actual run time using a clocking function.
3
3 Applications Sequential and binary search Rank sort, selection sort, bubble sort, and insertion sort Evaluating a polynomial using Horner’s rule Matrix operations such as add, transpose, and multiply
4
4 Performance of a program Performance of a program: the amount of computer memory and time needed to run the program Performance analysis: Use analytical methods to determine the performance of a program Performance measurement: Use experimental methods to determine the performance of a program
5
5 Space and time complexity Space complexity: the amount of memory needed to run the program Time complexity: the amount of computer time needed to run the program
6
6 Components of space complexity Instruction space Data space –Constants –Variables Environment stack space
7
7 Instruction space The compiler used The compiler options The target computer
8
8 Three possible codes for the evaluation of a+b+b*c+(a+b-c)/(a+b)+4
9
9 Data space
10
10 Data space for structural variables Double a[100];100*8 = 800 bytes int maze[rows][cols];2*rows*cols bytes
11
11 Environment stack The return address Local variables and value formal parameters The binding of all reference and const reference parameters
12
12 An example of recursive calls
13
13 Division of space needed S(P) = c + S p (instance characteristics) c - the fix part independent of the instance characteristics: instruction space, space for simple variables, fixed-size component variables, constants, etc S p - the variable part: space needed by component variables whose size depends on the particular problem instance, dynamically allocated space, and the recursion stack space
14
14 Example 1: Instance Characteristic is n c = 12 bytes when T = int; SSequentialSearch(n) = 0
15
15 Example 2: Assuming T is int, 6 bytes for each recursive call (to save formal parameters and return address. SRsum(n) = 6(n+1)
16
16 Time complexity Components of time complexity T(P) = compile time + run time (t p ) t p = c a ADD(n) + c s SUB(n) + c m MUL(n) + c d DIV(n) + … Time needed for an arithmetic operation depends on the type of data Machine dependent
17
17 Operation counts Select one or more operations, and determine how many of each is done Identify the operations that contribute most to the time complexity
18
18 Polynomial Evaluation P(x) = Σ i=0,n c i x n c n <> 0 The number of addition = n The number of multiplication = 2n
19
19 Horner’s rule: P(x) = (…(c n * x + c n-1 ) * x + c n-2 ) * x + c n-3 ) * x …) * x + c 0 The number of additions = n The number of multiplications = n
20
20 Ranking The rank of an element in a sequence is the number of smaller elements in the sequence plus the number of equal elements to its left. Example: –sequence [4, 3, 9, 3, 7] –Rank [2, 0, 4, 1, 3]
21
21 Ranking Total number of comparisons = 1 + 2 + 3 + … + n-1 = (n-1)n/2
22
22 Rank sort Comparisons: (n-1)n/2Moves: 2n
23
23 Selection Sort Need to sort list in ascending order: a[0] ≤ a[1] ≤ … ≤ a[n-1] –Determine largest element and swap it with a[n-1] –Determine the largest of the remaining n-1 elements and swap it with a[n-2] –And so on.
24
24 Selection Sort Comparisons: 1 + 2 + 3 + … + n-1 = (n-1)n/2 Moves: 3(n-1)
25
25 Bubble Sort Use “bubbling strategy” to get the largest element to the right. In each bubble pass: –Pairs of adjacent elements are compared –If left element is larger swap them –At end of pass, largest element ends up in rightmost position Bubble sort: repeat above to place each element in proper position
26
26 A bubbling pass Comparisons: n-1
27
27 Bubble Sort Comparisons: (n-1)n/2
28
28 Best, Worst, and Average Operation Counts Performance may depend on more than one factor op (n 1, n 2, …, n k ) is a function of the characteristics n 1, n 2, …, n k Let S(n 1, n 2, …, n k ) = {I | Instance I has the characteristics n 1, n 2, …, n k } O p BC (n 1, n 2, …, n k ) = min{operation p (I) | I є S(n 1, n 2, …, n k ) } O p WC (n 1, n 2, …, n k ) = max{operation p (I) | I є S(n 1, n 2, …, n k ) } O p AVG (n 1, n 2, …, n k ) = (1/|S(n 1, n 2, …, n k )|)Σ I є S(n1, n2, …, nk) operation p (I)
29
29 Insertion Comparisons: Minimum: 1; Maximum: n Average:
30
30 Rank Sort Revisited Swaps: Minimum: 0 Maximum: 2(n-1)
31
31 Selection Sort Revisited Comparisons: –Min: n-1 –Max: (n-1)n/2 Swaps: –Min: 0 –Max: n-1
32
32 Bubble Sort Revisited Comparisons: –Min: n-1 –Max: (n-1)n/2
33
33 Insertion Sort 1 Comparisons –Min: n-1 –Max: (n-1)n/2
34
34 Insertion Sort 2 Comparisons –Min: n-1 –Max: (n-1)n/2
35
35 End of Chapter 2 Part 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.