SNU IDB Lab. Ch4. Performance Measurement © copyright 2006 SNU IDB Lab.

Slides:



Advertisements
Similar presentations
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Advertisements

Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Fundamentals of Python: From First Programs Through Data Structures
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Chapter 1 – Basic Concepts
the fourth iteration of this loop is shown here
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Complexity Analysis (Part I)
Analysis of Algorithms. Time and space To analyze an algorithm means: –developing a formula for predicting how fast an algorithm is, based on the size.
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Insertion Sort for (i = 1; i < n; i++) {/* insert a[i] into a[0:i-1] */ int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j];
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Cache Memories May 5, 2008 Topics Generic cache memory organization Direct mapped caches Set associative caches Impact of caches on performance EECS213.
Time Complexity s Sorting –Insertion sorting s Time complexity.
Lec 5 Feb 10 Goals: analysis of algorithms (continued) O notation summation formulas maximum subsequence sum problem (Chapter 2) three algorithms image.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithm.
1 Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as a i,j and elements of.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
CHAPTER 1 BASIC CONCEPT All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures.
1 Performance Measurement CSE, POSTECH 2 2 Program Performance Recall that the program performance is the amount of computer memory and time needed to.
Insertion Sort for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1]
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Chapter 1 Algorithm Analysis
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Program Performance & Asymptotic Notations CSE, POSTECH.
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Lecture 2 Computational Complexity
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
External Sorting Sort n records/elements that reside on a disk. Space needed by the n records is very large.  n is very large, and each record may be.
SNU IDB Lab. Ch3. Asymptotic Notation © copyright 2006 SNU IDB Lab.
Complexity of Algorithms
Asymptotic Notation (O, Ω, )
Chapter 10 Algorithm Analysis.  Introduction  Generalizing Running Time  Doing a Timing Analysis  Big-Oh Notation  Analyzing Some Simple Programs.
Performance Measurement Performance Analysis Paper and pencil. Don’t need a working computer program or even a computer.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Analysis of algorithms. What are we going to learn? Need to say that some algorithms are “better” than others Criteria for evaluation Structure of programs.
Chapter 7 Analysis of Algorithms © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 Cache Memory. 2 Outline Cache mountain Matrix multiplication Suggested Reading: 6.6, 6.7.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
ANALYSING COSTS COMP 103. RECAP  ArrayList: add(), ensuring capacity, iterator for ArrayList TODAY  Analysing Costs 2 RECAP-TODAY.
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
Computer Science 320 A First Program in Parallel Java.
Algorithmics - Lecture 41 LECTURE 4: Analysis of Algorithms Efficiency (I)
Program Performance 황승원 Fall 2010 CSE, POSTECH. Publishing Hwang’s Algorithm Hwang’s took only 0.1 sec for DATASET1 in her PC while Dijkstra’s took 0.2.
1 Chapter 2 Program Performance. 2 Concepts Memory and time complexity of a program Measuring the time complexity using the operation count and step count.
1 Writing Cache Friendly Code Make the common case go fast  Focus on the inner loops of the core functions Minimize the misses in the inner loops  Repeated.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
(Complexity) Analysis of Algorithms Algorithm Input Output 1Analysis of Algorithms.
Complexity Analysis (Part I)
External Sorting Sort n records/elements that reside on a disk.
CS 213: Data Structures and Algorithms
Performance Measurement
Performance Measurement
Searching, Sorting, and Asymptotic Complexity
More on Asymptotic Analysis and Performance Measurement
Performance Measurement
More on Asymptotic Analysis + Performance Measurement
Performance Measurement
Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as ai,j and elements of B as.
Performance Measurement
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Presentation transcript:

SNU IDB Lab. Ch4. Performance Measurement © copyright 2006 SNU IDB Lab.

SNU IDB Lab. Data Structures 2 Preview of Chapters Chapter 2 How to analyze the space and time complexities of program Chapter 3 Review asymptotic notations such as O, Ω, Θ, o for simplifying the performance analysis Chapter 4 Show how to measure the actual run time of a program by using a clocking method

SNU IDB Lab. Data Structures 3 Bird’s eye view When you try to market your code Memory requirements is easy to figure out Running time requires need of experiments In this chapter How to perform such an experiment for run time Factors affecting running time The number and type of operations The memory access pattern for the data & instructions in your program

SNU IDB Lab. Data Structures 4 Table of Contents Introduction Choosing Instance Size Developing the Test Data Setting Up the Experiment Your Cache and You

SNU IDB Lab. Data Structures 5 Introduction Performance measurement Obtaining the actual space and time requirements of a program Dependent on the particular compiler and the specific computer Space requirements can be measured easily through the compiler and the analytical method Time requirements can be measured by Java method System.currentTimeMills() System.currentTimeMills() Returning the present time in millisecs since midnight (GMT), January 1, 1970 With the system clock, if we want to measure the worst case time requirements for sorting Need to decide the size of input data Need to determine the data that exhibit the worst case behavior

SNU IDB Lab. Data Structures 6 Table of Contents Introduction Choosing Instance Size Developing the Test Data Setting Up the Experiment Your Cache and You

SNU IDB Lab. Data Structures 7 Choosing Instance Size Want to measure the time of insertion sort for the array of n objects In theory, we know We can determine the quadratic function with three values of n In practice, we need the times for more than three values of n Asymptotic analysis tells us the behavior only for sufficiently large values of n Even in the region where the asymptotic behavior is exhibited, the times may not lie exactly on the predicted curve Reasonable choice of the input size N= 100, 200, …., 1000 N= 500, 1000, 1500, …. 5000

SNU IDB Lab. Data Structures 8 Insertion Sort N = 5, input sequence = (2, 3, 1, 5, 4) Making a sorted array using insertion Best case: n-1 comparisons Worst case: (n-1)*n / 2 comparisons

SNU IDB Lab. Data Structures 9 Table of Contents Introduction Choosing Instance Size Developing the Test Data Setting Up the Experiment Your Cache and You

SNU IDB Lab. Data Structures 10 Developing the Test Data Worst case or Best case Easy to generate the test data Average case Difficult to generate the test data If we cannot develop the test data showing the complexity Pick the least (maximum, average) measured time from randomly generated data as an estimate of the best (worst, average) behavior

SNU IDB Lab. Data Structures 11 Table of Contents Introduction Choosing Instance Size Developing the Test Data Setting Up the Experiment Your Cache and You

SNU IDB Lab. Data Structures 12 Setting Up the experiment: Program 4.1 public static void main(String [] args){ int step = 10; System.out.println("The worst-case times, in milliseconds, are"); System.out.println("n \telapsed time"); for (int n = 0; n <= 1000; n += step) { Integer [] a = new Integer[n]; // create element array for (int i = 0; i < n; i++) // initialize array a[i] = new Integer(n - i); long startTime = System.currentTimeMillis() ; InsertionSort2.insertionSort(a); // sort the elements long elapsedTime = System.currentTimeMillis() - startTime; System.out.println(n + "\t" + elapsedTime); if (n == 100) step = 100; } }

SNU IDB Lab. Data Structures 13 Execution Times using program 4.1

SNU IDB Lab. Data Structures 14 Experiment Accuracy Accuracy of measurements When n is small, measured time can be inaccurate because of an error tolerance Error tolerance of System.currentTimeMills() To improve the accuracy upto 10% Elapsed time should be 1000 msecs For different data sizes Repeat the program upto 1000 msecs Measure the average

SNU IDB Lab. Data Structures 15 Insertion Sort Exp with 10% accuracy (1) public static void main(String [] args) { int step = 10; // intially data size 10, 20,… System.out.println("The worst-case times, in milliseconds, are"); System.out.println("n repetitions elapsed time time/sort"); for (int n = 0; n <= 1000; n += step) { Integer [] a = new Integer[n]; // create element array long startTime = System.currentTimeMillis() ; long counter = 0; do { counter++; for (int i = 0; i < n; i++) a[i] = new Integer(n - i); // initialize array InsertionSort2.insertionSort(a); // sort the elements } while(System.currentTimeMillis( ) - startTime < 1000); // keep going upto 1000 msecs long elapsedTime = System.currentTimeMillis() - startTime; System.out.println (n + " " + counter + " " + elapsedTime + " " + ((float) elapsedTime)/counter if (n == 100) step = 100; // after 100, data size  100, 200, 300, … }

SNU IDB Lab. Data Structures 16 Insertion Sort Exp with 10% accuracy (2) * fixed given time * fixed given data

SNU IDB Lab. Data Structures 17 Table of Contents Introduction Choosing Instance Size Developing the Test Data Setting Up the Experiment Your Cache and You

SNU IDB Lab. Data Structures 18 A Simple Computer Model (1/2) Consider a simple computer model ALU R L1 L2 main memory ALU: Arithmetic Logical Unit R: Register L1: Level 1 Cache L2: Level 2 Cache

SNU IDB Lab. Data Structures 19 A Simple Computer Model (2/2) Cycle of our model Time needed to load data 2 cycles, L1  R 10 cycles, L2  L1  R 100 cycles, main memory  L2  L1  R Add 1 cycle, R  ALU Store 1 cycle, write operation in memory

SNU IDB Lab. Data Structures 20 Effect of Cache Misses on Run Time Compiling “a = b + c” load b; load c; add; store a add, store 1 cycle each load No cache miss  2*2 cycles  total 4 cycles Every cache miss  100*2 cycles  total 202 cycles Run time depends on cache miss!

SNU IDB Lab. Data Structures 21 Matrix multiplication (1/5) Matrix multiplication Rows of Matrix are stored adjacently in memory i k k j i j *=

SNU IDB Lab. Data Structures 22 Matrix multiplication (2/5) Multiplication of two square matrices Position of elements in the memory Same row  adjacent Same column  apart

SNU IDB Lab. Data Structures 23 Matrix multiplication (3/5) public static void fastSquareMultiply(int [][]a, int [][] b,int [][] c, int n) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) c[i][j] = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) c[i][j] += a[i][k] * b[k][j]; } A(3,3) * B(3,2) = C(3,2) What if we exchange j and k

SNU IDB Lab. Data Structures 24 Matrix multiplication (4/5) For loop order ijk order Elements of a, c are accessed by row Elements of b are accessed by column Probability of cache miss ikj order All elements of a, b, c are accessed by row It will take less time

SNU IDB Lab. Data Structures 25 Matrix multiplication (5/5) Run times for matrix multiplication ikj order takes 10% less time nijk orderikj order

SNU IDB Lab. Data Structures 26 Observation Matrix multiplication Knowledge about computer architecture Memory access pattern of matrix multiplication Simple idea about data positioning  a very fundamental data structure technique Performance vs. Data structure

SNU IDB Lab. Data Structures 27 Summary When you try to market your code Memory requirements is easy to figure out Running time requires need of experiments In this chapter How to perform such an experiment for run time Factors affecting running time The number and type of operations The memory access pattern for the data & instructions in your program