Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -

Similar presentations


Presentation on theme: "CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -"— Presentation transcript:

1 CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -
Space complexity Instance characteristics Time complexity Computing the rank of an element Rank sort Horner’s rule for polynomial evaluation CSE 373, Copyright S. Tanimoto, Performance Analysis -

2 CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -
Warm-up Activity Write in Java a method to take a Vector of objects as input, along with one separate object, and determine the index of the first occurrence, if any, of that object in the vector. But do NOT use the built in method indexOf. Use elementAt(int i), size(). CSE 373, Copyright S. Tanimoto, Performance Analysis -

3 Some Reference Code: A Loop to List the List the Elements of a Vector
void listElements(Vector v) { for (int i=0; i < v.size(); i++) { System.out.println( v.elementAt(i)) } CSE 373, Copyright S. Tanimoto, Performance Analysis -

4 One solution to the rank problem.
int rank(Vector v, Object ob) { for (int i=0; i<v.size(); i++) { if (v.elementAt(i).equals(ob)) return i; } return -1; CSE 373, Copyright S. Tanimoto, Performance Analysis -

5 CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -
Space Complexity Amount of memory required to run some program or algorithm. Instruction space Data space Environment stack space c + SP(instance characteristics) CSE 373, Copyright S. Tanimoto, Performance Analysis -

6 Instance Characteristics (Definition)
Suppose we have a class of problems, such as (a) sorting lists of integers or (b) finding all misspelled words in documents. An instance characteristic is a measure (typically with integer value) of the magnitude of an instance of the problem. It's essentially a function whose domain is a set of problems and whose range is the set of nonnegative integers. CSE 373, Copyright S. Tanimoto, Performance Analysis -

7 Instance Characteristics (Some of the Alternatives)
n = number of data items to be sorted. n = magnitude of an input parameter. n = number of bits used to represent the input ( e.g., log2 magnitude of parameter) n = max of width and height of an image (in pixels) n = number of pixels in the image CSE 373, Copyright S. Tanimoto, Performance Analysis -

8 Instance Characteristics (A more interesting example)
The class of problems: "Web site analysis" Given a home page, find all pages within the same site that are reachable via links, and index each word found. What is an appropriate instance characteristic? (Discussion in class) CSE 373, Copyright S. Tanimoto, Performance Analysis -

9 CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -
Time Complexity How should we describe the running time of an algorithm? Using the number of seconds it takes to run on a particular problem and machine? CSE 373, Copyright S. Tanimoto, Performance Analysis -

10 Time Complexity: Issues & Approaches
Non-algorithmic factors: hardware, OS, compiler. Abstract measures: Virtual machines (e.g., JVM) Operation counts Asymptotic growth -- Big O notation. CSE 373, Copyright S. Tanimoto, Performance Analysis -

11 CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -
Runtime tP(instance characteristics) tP(n) = caADD(n) + csSUB(n) + cmMUL(n) + cdDIV(n) + ... CSE 373, Copyright S. Tanimoto, Performance Analysis -

12 CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -
Operation Counts Choose a key operation as representative of the dependency of computation time on instance characteristics. e.g., Comparison: "Is x < y ?" (calls to CompareTo operations -- Sahni, p.40) e.g., all Arithmetic operations count 1. CSE 373, Copyright S. Tanimoto, Performance Analysis -

13 CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -
Rank of an Element Rank of an element E in an array A is the number of elements of A less than E plus the number of elements equal to E appearing to the left of E. Let A = [3, 7, 4, 2, 1, 9, 3] Rank of 7 in A is 5. Rank of the last 3 in A is 3. All ranks of A: [2, 5, 4, 1, 0, 6, 3] CSE 373, Copyright S. Tanimoto, Performance Analysis -

14 CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -
Computing the Ranks public static void rank(Comparable [] a, int [] r) { if (r.length < a.length) throw new IllegalArgumentException ("Rank array too short"); for (int i = 0; i < a.length; i++) r[i] = 0; for (int i = 1; i < a.length; i++) { for (int j = 0; j < i; j++) if (a[j].compareTo(a[i])) <= 0) r[i]++; else r[j]++; } //(Sahni, p.79) CSE 373, Copyright S. Tanimoto, Performance Analysis -

15 CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -
Performance of rank Assume n = a.length = r.length; Space: Assume 1 unit of storage for each array element (but note that rank does not allocate these arrays.) Space = c + 2 * n Time: Assume we count “object” comparisons only. Time = n * (n - 1) / 2 CSE 373, Copyright S. Tanimoto, Performance Analysis -

16 CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -
Rearranging public static void rearrange(Comparable [] a, int [] r) { Comparable [] u = new Comparable [a.length]; // Sort into u using ranks: for (int i = 0; i < a.length; i++) { u[r[i]] = a[i]; // Copy items back to array a. a[i] = u[i]; } //(Sahni, p.80) CSE 373, Copyright S. Tanimoto, Performance Analysis -

17 Performance of rearrange
Space: n additional memory locations (for array u) Time: Assume we count object assignments only. Time = n * 2 Overall time for sorting by ranking and rearranging must be expressed with a unit that is the same for each piece. So now let’s consider both comparisons and assignments: Sorting time = n * (n - 1)/2 Comp + 2 * n Assign. CSE 373, Copyright S. Tanimoto, Performance Analysis -

18 Polynomial Evaluation
P(x) = c0xn + c1xn cn-1 x + cn Suppose we are given a value for x. How should we compute P(x) ? CSE 373, Copyright S. Tanimoto, Performance Analysis -

19 Polynomial Evaluation: One Way
P(x) = c0xn + c1xn cn-1 x + cn A slow method: first compute xn and multiply it by c0 Next compute x n-1 and multiply it by c1, etc. This requires more than (n - 1) (n - 2) / 2 multiplications. A more direct Method: Compute x, x2, x3, etc. saving them. Then multiply each by its corresponding coefficient and add the products. This requires n memory locations to store the powers, and it requires 2n - 1 multiplications. CSE 373, Copyright S. Tanimoto, Performance Analysis -

20 Polynomial Eval. w/ Horner’s Rule
P(x) = c0xn + c1xn cn-1 x + cn Compute c0x Add c1 getting t1 Computer t1x Add c2 getting t2 Compute t2x ... Add cn getting the answer. Requires 1 memory location to hold current result ti, and needs n multiplications (and n additions). CSE 373, Copyright S. Tanimoto, Performance Analysis -


Download ppt "CSE 373, Copyright S. Tanimoto, 2002 Performance Analysis -"

Similar presentations


Ads by Google