Performance Measurement

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

Fundamentals of Python: From First Programs Through Data Structures
Introduction to Analysis of Algorithms
Performance Measurement Performance Analysis Paper and pencil. Don’t need a working computer program or even a computer.
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
Time Complexity s Sorting –Insertion sorting s Time complexity.
27-Jun-15 Profiling code, Timing Methods. Optimization Optimization is the process of making a program as fast (or as small) as possible Here’s what the.
Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
1 Performance Measurement CSE, POSTECH 2 2 Program Performance Recall that the program performance is the amount of computer memory and time needed to.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Algorithms and Algorithm Analysis The “fun” stuff.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Asymptotic Notation (O, Ω, )
Performance Measurement Performance Analysis Paper and pencil. Don’t need a working computer program or even a computer.
SNU IDB Lab. Ch4. Performance Measurement © copyright 2006 SNU IDB Lab.
Processor Structure and Function Chapter8:. CPU Structure  CPU must:  Fetch instructions –Read instruction from memory  Interpret instructions –Instruction.
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.
COSC 1P03 Data Structures and Abstraction 2.1 Analysis of Algorithms Only Adam had no mother-in-law. That's how we know he lived in paradise.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
TAs Fardad Jalili: Aisharjya Sarkar: Soham Das:
Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
LECTURE 9 CS203. Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search) and sorting (selection sort.
Complexity Analysis (Part I)
Outline lecture Revise arrays Entering into an array
May 17th – Comparison Sorts
Analysis of Algorithms
Lecture 16: Data Storage Wednesday, November 6, 2006.
Introduction to complexity
Big-O notation.
Search Lesson Outline Searching Lesson Outline
Assignment 2 Tze calculations.
Source: wikipedia
Lecture 3 of Computer Science II
Introduction to Algorithms
Source:
Algorithm Analysis CSE 2011 Winter September 2018.
Course Description Algorithms are: Recipes for solving problems.
Performance Measurement
Big-Oh and Execution Time: A Review
Algorithm Analysis (not included in any exams!)
Building Java Programs
CSE 143 Lecture 5 Binary search; complexity reading:
Insertion Sort for (int i = 1; i < a.length; i++)
Quicksort analysis Bubble sort
ITEC 2620M Introduction to Data Structures
CSC 413/513: Intro to Algorithms
Lesson 15: Processing Arrays
Performance Measurement
An Overview of Insertion Sort
Data Structures Review Session
CS 201 Fundamental Structures of Computer Science
Building Java Programs
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Sorting Rearrange a[0], a[1], …, a[n-1] into ascending order.
CSE 373 Data Structures and Algorithms
Analysis of Algorithms
CSC 427: Data Structures and Algorithm Analysis
Insertion Sort for (int i = 1; i < n; i++)
Analysis of Algorithms
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Performance Measurement
Course Description Algorithms are: Recipes for solving problems.
Estimating Algorithm Performance
Insertion Sort for (int i = 1; i < n; i++)
CSE 332: Data Abstractions Memory Hierarchy
Performance Measurement
Complexity Analysis (Part I)
Analysis of Algorithms
Complexity Analysis (Part I)
Presentation transcript:

Performance Measurement

Performance Analysis Paper and pencil. Don’t need a working computer program or even a computer. These could be considered some of the advantages of performance analysis over performance measurement.

Some Uses Of Performance Analysis determine practicality of algorithm predict run time on large instance compare 2 algorithms that have different asymptotic complexity e.g., O(n) and O(n2) If the complexity is O(n2), we may consider the algorithm practical even for large n. An O(2n) algorithm is practical only for small n, say n < 40. The worst-case run time of an O(n2) algorithm will quadruple with each doubling of n. So if the worst-case time is 10 sec when n = 100, it will be approximately 40 sec when n = 200.

Limitations of Analysis Doesn’t account for constant factors. but constant factor may dominate 1000n vs n2 and we are interested only in n < 1000

Limitations of Analysis Modern computers have a hierarchical memory organization with different access time for memory at different levels of the hierarchy.

Memory Hierarchy MAIN L2 L1 ALU R 8-32 32KB 512KB 512MB 1C 2C 10C 100C

Limitations of Analysis Our analysis doesn’t account for this difference in memory access times. Programs that do more work may take less time than those that do less work. A program that does 100 operations on the same data would take less time than A program that performs a single operation on say 50 different pieces of data (assuming, in a simple model) that each time data is fetched from main memory, only one piece of data is fetched. The first program would make one fetch at a cost of 100 cycles and perform 100 operations at a cost of 1 cycle each for a total of 200 cycles. The second program would need 50 * 100 cycles just to fetch the data.

Performance Measurement Measure actual time on an actual computer. What do we need?

Performance Measurement Needs programming language working program computer compiler and options to use javac -o

Performance Measurement Needs data to use for measurement worst-case data best-case data average-case data timing mechanism --- clock

Timing In Java long startTime = System.currentTimeMillis(); // gives time in milliseconds since 1/1/1970 GMT // code to be timed comes here long elapsedTime = System.currentTimeMillis() - startTime;

Shortcoming Clock accuracy assume 100 milliseconds Repeat work many times to bring total time to be >= 1 second Preceding measurement code is acceptable only when the elapsed time is large relative to the accuracy of the clock.

Accurate Timing long startTime = System.currentTimeMillis(); long counter; do { counter++; doSomething(); } while (System.currentTimeMillis() - startTime < 1000) long elapsedTime = System.currentTimeMillis() - startTime; float timeForMethod = ((float) elapsedTime)/counter;

Accuracy Now accuracy is 10%. first reading may be just about to change to startTime + 100 second reading may have just changed to finishTime so finishTime - startTime is off by 100ms

Accuracy first reading may have just changed to startTime second reading may be about to change to finishTime + 100 so finishTime - startTime is off by 100ms

Accuracy Examining remaining cases, we get trueElapsedTime = finishTime - startTime +- 100ms To ensure 10% accuracy, require elapsedTime = finishTime – startTime >= 1sec

What Went Wrong? long startTime = System.currentTimeMillis(); long counter; do { counter++; InsertionSort.insertionSort(a); } while (System.currentTimeMillis() - startTime < 1000) long elapsedTime = System.currentTimeMillis() - startTime; float timeForMethod = ((float) elapsedTime)/counter; Suppose we are measuring worst-case run time. The array a Initially is in descending order. After the first iteration, the data is sorted. Subsequent iterations no longer start with worst-case data. So the measured time is for one Worst-case sort plus counter-1 best-case sorts!

The Fix long startTime = System.currentTimeMillis(); long counter; do { counter++; // put code to initialize a[] here InsertionSort.insertionSort(a); } while (System.currentTimeMillis() - startTime < 1000) Now the measured time is for counter number of worst-case sorts plus counter number of initializes. We can measure the time to initialize separately and subtract. Or, if We are to compare with other sort methods, we could ignore the initialize time as it is there in the measurements for all sort methods. Or we could argue that for large n the worst case time to sort using insertion sort (O(n^2)) overshadows the initialize time, which is O(n), and so we may ignore the initialize time.

Time Shared System UNIX time MyProgram Using System.currentTimeMillis() is a problem, because your program may run for only part of the physical time that has elapsed. The UNIX command time gives the time for which your program ran.

Bad Way To Time elapsedTime += System.currentTimeMillis() - startTime; do { counter++; startTime = System.currentTimeMillis(); doSomething(); elapsedTime += System.currentTimeMillis() - startTime; } while (elapsedTime < 1000) Suppose the clock is updated every 100ms and doSomething takes 99ms and it takes another 1ms do do everything else in the loop. If the clock is updated exactly at the start of the loop then it is not updated between the startTime = and elapsedTime += statements. So elapsedTime is always 0.