
4.1 Performance Analysis Running Time The Challenge “As soon as an Analytic Engine exists, it will necessarily guide the future course of the science. Whenever any result is sought by its aid, the question will arise - By what course of calculation can these results be arrived at by the machine in the shortest time?” – Charles Babbage debug solve problems compile how many times do you on test cases in practice have to turn the crank? Q. Will my program be able to solve a large practical problem? Charles Babbage (1864) Analytic Engine Key insight. [Knuth 1970s] Use the scientific method to understand performance. 3 4 Scientific Method Reasons to Analyze Algorithms Scientific method. Predict performance. • Observe some feature of the natural world. • Will my program finish? • Hypothesize a model that is consistent with the observations. • When will my program finish? • Predict events using the hypothesis. • Verify the predictions by making further observations. Compare algorithms. • Validate by repeating until the hypothesis and observations agree. • Will this change make my program faster? • How can I make my program faster? Principles. • Experiments must be reproducible; Basis for inventing new ways to solve problems. • Hypotheses must be falsifiable. • Enables new technology. • Enables new research. 5 6 Algorithmic Successes Algorithmic Successes N-body Simulation. Discrete Fourier transform. • Simulate gravitational interactions among N bodies. • Break down waveform of N samples into periodic components. • Brute force: N2 steps. • Applications: DVD, JPEG, MRI, astrophysics, …. 2 John Tukey Barnes-Hut: N log N steps, enables new research. Andrew Appel Brute force: N steps. • • 1965 PU '81 • FFT algorithm: N log N steps, enables new technology. (N 2) (N 2) (N log N) (N log N) number of bodies number of samples 7 8 Example: Three-Sum Problem Three-Sum public class ThreeSum Three-sum problem. Given N integers, find triples that sum to 0. { Application. Deeply related to problems in computational geometry. // Return number of distinct triples (i, j, k) // such that (a[i] + a[j] + a[k] == 0) public static int count(int[] a) { int N = a.length; % more 8ints.txt 30 -30 -20 -10 40 0 10 5 int cnt = 0; for (int i = 0; i < N; i++) all possible triples i < j < k % java ThreeSum < 8ints.txt for (int j = i+1; j < N; j++) 4 for (int k = j+1; k < N; k++) 30 -30 0 if (a[i] + a[j] + a[k] == 0) cnt++; 30 -20 -10 -30 -10 40 return cnt; -10 0 10 } public static void main(String[] args) { int[] a = StdArrayIO.readInt1D(); int result = count(a); StdOut.println(result); TEQ. Write a program to solve this problem. } } 9 10 Empirical Analysis Empirical Analysis Empirical analysis. Run the program for various input sizes. N time (1970) 1 time (2010) 2 500 62 0.03 1,000 531 0.26 2,000 4322 2.16 4,000 34377 17.18 8,000 265438 137.76 1. Time in seconds on Jan 18, 2010 running Linux on Sun-Fire-X4100 with 16GB RAM 2. Time in seconds in 1970 running MVT on IBM 360/50 with 256 KB RAM (estimate) 12 Stopwatch Stopwatch Q. How to time a program? Q. How to time a program? A. A stopwatch. A. Use Java’s System.currentTimeMillis() method. public static void main(String[] args) { int[] a = StdArrayIO.readInt1D(); int then = System.currentTimeMillis(); int result = count(a); int now = System.currentTimeMillis(); StdOut.println(result); StdOut.println((now - then)/1000.0); } 13 14 Data Analysis Data Analysis Data analysis. Plot running time vs. input size N. Data analysis. Plot running time vs. input size N on a log-log scale straight line of 150 300000 slope 3 7 N T(N) lg(N) lg(T(N)) 100 200000 4 1,000 0.26 10 -1.9 3 2 lg(time) 2,000 2.16 11 1.1 time (seconds) 1 time (seconds) 50 100000 4,000 17.18 12 4.1 8,000 137.76 13 7.1 -2 0 0 0 2000 4000 8000 0 2000 4000 8000 lg (T(N)) = 3 lg N + a input size N input size N 10 11 12 13 2010 1970 lg (N) T(N) = a N 3 Hypothesis. Running times on different computers differ by a constant factor. Hypothesis: Running time grows as the cube of the input size: a N 3 Q. How does running time grow as a function of input size N ? machine-dependent 15 constant factor 16 Prediction and verification Doubling hypothesis Hypothesis. Running time is about a N 3 for input of size N. Doubling hypothesis. Quick two-step method for prediction. Implicit hypothesis: Running times on different computers Hypothesis: T(2N)/T(N) approaches a constant. differ by a constant factor Q. How to estimate a ? N T(N) A. Solve for it! Step 1: Run program, doubling input size, N T(N) ratio 1,000 0.26 to find the constant 500 0.03 - 3 137.76 = a × 8000 2,000 2.16 ⇒ a = 2.7 × 10 –10 Step 2: Extrapolate to predict next entries 1,000 0.26 7.88 4,000 17.18 2,000 2.16 8.43 8,000 137.76 Consistent with power law hypothesis 4,000 17.18 7.96 a(2N)b / aNb = 2b 8,000 137.76 7.96 –10 3 Refined hypothesis. Running time is about 2.7 × 10 × N seconds. seems to (exponent is lg of ratio) converge to 8 Prediction. 1,100 seconds for N = 16,000. 16,000 1102 8 32,000 8816 8 137.76*8 Admits more functions ... ... Observation. 1102*8 N time (seconds) validates hypothesis! Ex. T(N) = N lg N 512,000 36112957 16000 1110.73 a(2N lg 2N) / aN lg N = 2 + 1/(lg N) ➞ 2 8816*84 17 18 TEQ on Performance 1 TEQ on Performance 2 Let F(N) be the running time of program Mystery for input N. Let F(N) be the running time of program Mystery for input N. public static Mystery public static Mystery { { ... ... int N = Integer.parseInt(args[0]); int N = Integer.parseInt(args[0]); ... ... } } N T(N) ratio N T(N) ratio Observation: Observation: 1,000 4 1,000 4 2,000 15 4 2,000 15 4 4,000 60 4 4,000 60 4 8,000 240 4 8,000 240 4 Q. Predict the running time for N = 128,000 Q. Order of growth of the running time? 19 20 Mathematical models for running time Total running time: sum of cost × frequency for all operations. Mathematical Analysis • Need to analyze program to determine set of operations. • Cost depends on machine, compiler. • Frequency depends on algorithm, input data. Donald Knuth 1974 Turing Award In principle, accurate mathematical models are available. 22 Example: 1-sum Example: 2-sum Q. How many instructions as a function of N? Q. How many instructions as a function of N? int count = 0; int count = 0; for (int i = 0; i < N; i++) for (int i = 0; i < N; i++) if (a[i] == 0) count++; for (int j = i+1; j < N; j++) if (a[i] + a[j] == 0) count++; 1 0+1+2+...+(N 1) = N (N 1) operation frequency operation frequency − 2 − N = 2 variable declaration 2 variable declaration N + 2 ⇥ assignment statement 2 assignment statement N + 2 less than compare N + 1 less than compare 1/2 (N + 1) (N + 2) equal to compare N equal to compare 1/2 N (N − 1) between N (no zeros) tedious to count exactly array access N and 2N (all zeros) array access N (N − 1) increment ≤ 2 N increment ≤ N 2 23 24 Tilde notation Example: 2-sum • Estimate running time (or memory) as a function of input size N. Q. How long will it take as a function of N? • Ignore lower order terms. - when N is large, terms are negligible int count = 0; - when N is small, we don't care for (int i = 0; i < N; i++) for (int j = i+1; j < N; j++) if (a[i] + a[j] == 0) count++; "inner loop" Ex 1. 6 N 3 + 20 N + 16!! ~ 6 N 3 Ex 2. 6 N 3 + 100 N 4/3 + 56! ~ 6 N 3 Ex 3. 6 N 3 + 17 N 2 lg N + 7 N! ~ 6 N 3 operation frequency time per op total time variable declaration ~ N c1 ~ c1 N discard lower-order terms assignment statement ~ N c2 ~ c2 N (e.g., N = 1000: 6 billion vs. 169 million) less than comparison ~ 1/2 N 2 c3 ~ c3 N 2 equal to comparison ~ 1/2 N 2 f (N) depends on Technical definition. f(N) ~ g(N) means lim = 1 array access ~ N 2 c4 ~ c4 N 2 input data N → ∞ g(N) increment ≤ N 2 c5 ≤ c5 N 2 total ~ c N 2 € 25 depends on machine 26 Example: 3-sum Mathematical models for running time Q. How many instructions as a function of N? In principle, accurate mathematical models are available. In practice, int count = 0; ~ 1 Formulas can be complicated. for (int i = 0; i < N; i++) • ~ N Advanced mathematics might be required. for (int j = i+1; j < N; j++) • ~ N 2 / 2 for (int k = j+1; k < N; k++) • Exact models best left for experts. N N(N 1)(N 2) "inner loop" if (a[i] + a[j] + a[k] == 0) = − − 3 3! ⇥ count++; 1 costs (depend on machine, compiler) N 3 ⇥ 6 TN = c1 A + c2 B + c3 C + c4 D + c5 E may be in inner loop, depends on input data A = variable declarations B = assignment statements frequencies C = compare (depend on algorithm, input) D = array access E = increment Remark.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages12 Page
-
File Size-