Software Analysis. Asymptotic Performance Analysis

Similar documents
CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Analysis of Algorithms Part I: Analyzing a pseudo-code

CS2 Algorithms and Data Structures Note 1

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

Introduction to the Analysis of Algorithms. Algorithm

Computer Algorithms. Introduction to Algorithm

Chapter 2: Algorithm Complexity Analysis. Chaochun Wei Spring 2018

COMP6700/2140 Recursive Operations

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY

Algorithmic Analysis. Go go Big O(h)!

Analysis of Algorithms - Introduction -

Lecture 5 Sorting Arrays

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Fundamental Algorithms

What is an algorithm?

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Algorithm Analysis CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

Measuring algorithm efficiency

Algorithm Analysis. CENG 707 Data Structures and Algorithms

CS 216 Fall 2007 Final Exam Page 1 of 10 Name: ID:

Chapter 2: Complexity Analysis

Algorithms and Data Structures

Lesson 1 1 Introduction

Introduction to Analysis of Algorithms

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Remember, to manage anything, we need to measure it. So, how do we compare two implementations? ArrayBag vs. LinkedBag Which is more efficient?

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 2 Analysis of Fork-Join Parallel Programs

Algorithm. Lecture3: Algorithm Analysis. Empirical Analysis. Algorithm Performance

Lecture Notes on Sorting

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

Parallel Constraint Programming (and why it is hard... ) Ciaran McCreesh and Patrick Prosser

Data Structures and Algorithms. Part 2

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

Lecture Notes for Chapter 2: Getting Started

Data Structures Lecture 3 Order Notation and Recursion

CSCA48 Winter 2018 Week 10:Algorithm Analysis. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

Coventry University

Algorithm Analysis and Design

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

EECS 477: Introduction to algorithms. Lecture 7

Data Structures and Algorithms Chapter 2

Checking for duplicates Maximum density Battling computers and algorithms Barometer Instructions Big O expressions. John Edgar 2

CSE 332: Data Structures & Parallelism Lecture 15: Analysis of Fork-Join Parallel Programs. Ruth Anderson Autumn 2018

CS 261 Data Structures. Big-Oh Analysis: A Review

Forward recursion. CS 321 Programming Languages. Functions calls and the stack. Functions calls and the stack

ASYMPTOTIC COMPLEXITY

ASYMPTOTIC COMPLEXITY

Algorithms and Data Structures, or

Organisation. Assessment

Basic Data Structures and Heaps

The Running Time of Programs

Introduction to Programming I

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 2 Analysis of Fork-Join Parallel Programs

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

COMP 161 Lecture Notes 16 Analyzing Search and Sort

Programming II (CS300)

CS 137 Part 7. Big-Oh Notation, Linear Searching and Basic Sorting Algorithms. November 10th, 2017

How to Write Fast Numerical Code

EECS 477: Introduction to algorithms. Lecture 6

Outline and Reading. Analysis of Algorithms 1

Introduction to Parallel Performance Engineering

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

Test 1 SOLUTIONS. June 10, Answer each question in the space provided or on the back of a page with an indication of where to find the answer.

Cilk, Matrix Multiplication, and Sorting

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

CSC630/CSC730 Parallel & Distributed Computing

Algorithm Analysis. Big Oh

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

Algorithms A Look At Efficiency

Optimisation and Operations Research

Unit #3: Recursion, Induction, and Loop Invariants

arxiv: v3 [cs.ds] 3 Apr 2018

IT 4043 Data Structures and Algorithms

Advanced R Programming - Lecture 6

CSCI 104 Runtime Complexity. Mark Redekopp David Kempe

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

6. Pointers, Structs, and Arrays. 1. Juli 2011

Running Time. Analytic Engine. Charles Babbage (1864) how many times do you have to turn the crank?

CS 31: Intro to Systems Caching. Martin Gagne Swarthmore College March 23, 2017

Lecture 4: Parallel Speedup, Efficiency, Amdahl s Law

Principles of Algorithm Analysis. Biostatistics 615/815

How to Write Fast Numerical Code

Computer Systems A Programmer s Perspective 1 (Beta Draft)

CS1 Lecture 22 Mar. 6, 2019

Performance Op>miza>on

Introduction to Computer Science

ECE 2400 Computer Systems Programming Fall 2018 Topic 8: Complexity Analysis

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

Algorithms I Dr Robert Harle. CST Paper I (IA NST CS, PPS CS and CST) Easter 2009/10

Transcription:

Software Analysis Performance Analysis Presenter: Jonathan Aldrich Performance Analysis Software Analysis 1 Asymptotic Performance Analysis How do we compare algorithm performance? Abstract away low-level details E.g. exact assembly code Investigate scaling behavior Which is better on really large inputs? For small inputs, any algorithm will do Asymptotic performance As input size grows, how does execution time grow? 2 1

Counting Operations Input size N Number of elements of array to be searched or sorted Dimensions of matrix to be multiplied Number of bits in a binary number Idea: count operations performed Ignore constant factors An operation can be anything that is implemented in a fixed number of assembly operations Let T(N) = number of operations for input size N Could be average or maximum May be complex to calculate Say T(N) = 2 N + 2N 2 + 10 Do we really care about the constant 10? If N is big all that matters is 2 N 3 Big-O Notation Notation taken from notes by Dan Ellard Order of algorithm T(N) = O(f(N)) There exist positive constants c and n 0 For all N n 0 we have T(N) c f(n) Example 2 N+1 + 2N 2 + 10 = O(2 N ) Choose c = 4 and n 0 = 5 2 5+1 + 2(5) 2 + 10 = 64 + 50 + 10 = 124 4 * 2 5 = 4 * 32 = 128 We can prove inductively that this is true for all N 5 4 2

Big-O Rules If T 1 (N) = O(f 1 (N)) and T 2 (N) = O(f 2 (N)) T 1 (N) + T 2 (N) = O(max(f 1 (N), f 2 (N))) E.g. N 2 +N = O(max(N 2, N)) = O(N 2 ) T 1 (N) * T 2 (N) = O(f 1 (N) * f 2 (N)) E.g. N*(N 2 +N) = O(N * N 2 ) = O(N 3 ) Domination of functions Exponentials dominate polynomials Polynomials dominate logarithms Logarithms dominate constants If a function has several terms, only consider the order of the dominant term By this shortcut we know 2 N+1 + 2N 2 + 10 = O(2 N ) Caveat: N*2 N+1 + 2N 2 + 10 = O(N*2 N ), not O(2 N ) Can ignore terms that are added but not terms that are multiplied 5 Check Your Understanding (1) 2N 2 + 3N + 5 = O( ) N + log N = O( ) 2 log N + 100 = O( ) 2 N/2 + 100000N 25 = O( ) 6 3

How to Analyze an Algorithm Straightline code Always O(1)! Code with loops How many times might each loop execute? Multiply bounds for nested loops Add bounds for sequential loops Recursive functions How many times is the function called? How many operations per function, counting each recursive call as a single operation? (the recursive call is accounted for separately) Multiply the two numbers above Sometimes better to count operations Bounds based on loops or recursion could be too loose 7 Check Your Understanding (2) if (x == y) x := x + 200 else y := y * 8 O( ) for (int i = 0; i < N; ++i) for (int j = 0; j < i; ++j) x := x + j for (int k = 0; k < 2*N; ++k) x := x - k O( ) 8 4

Check Your Understanding (2) int foo(int N) { if (N == 0) return 1; else return foo(n-1) + foo(n-1); } O( ) // constant-time hashtable operations int lookup(int N) { } void store(int N, int V) { } int foo(int N) { if (N == 0) return 1; val = lookup(n); if (val == -1) { val = foo(n-1) + foo(n-1); store(n, val); } return val; } O( ) 9 Optimization Making code run fast is fun! But beware Premature optimization is the root of all evil. Donald Knuth (CC2, p. 594) 10 5

Cost of Optimization Optimizing your program may reduce other quality attributes Evolvability Optimizations may increase coupling Understandability Optimizations obscure high-level design On-Time Delivery Optimizations may delay the project s delivery Reliability Optimizations may make your code less likely to be correct (because it is more complex) Don t optimize at all unless you know you need to! 11 Do you really need speed? TRW system Original requirement: sub-second response time Cost estimate: $100 million Revised requirement: 4 second response time is OK Cost estimate $30 million What level of performance can your users live with? Can you achieve the necessary performance by buying more hardware? Maybe not if you are buying 10 million widgets; otherwise probably yes! 12 6

Optimization Your boss tells you, make it run faster. What do you do? 13 Speedup Analysis Scenario Assumptions Procedure x takes 75% of execution time We can speed up x by a factor of 3 How much faster will the program run? 14 7

Amdahl s Law P = % of program you can speed up S = speedup of that part of the program Maximum overall speedup: 1 (1-P) + P/S time to run rest of program time to run sped-up program part 15 80/20 rule (Pareto principle) 20% of the program takes 80% of the time No point in optimizing anything until you know what 20% is causing the problem! Another way of stating Amdahl s law 16 8

Getting Good Performance What matters? What doesn t? 17 Getting Good Performance Algorithms #1 way to optimize your code If you pick an O(n 2 ) algorithm instead of O(n), nothing else (typically) matters Cache performance Penalty for a cache miss may be hundreds of cycles Know your data layout and cache architecture Access multiple words in the same cache line together Try to access your data in groups that fit into the size of the cache Parallelism Your computer likely has multiple processors use them if you can Caching Trade space for time, e.g. to avoid recomputing results Doing less work Trade precision for time, e.g. by using approximations 18 9

What the compiler should do for you Inlining calls E.g. accessor functions like getx() C++ allows you to recommend inlining Often an optimizing compiler is smarter than you are Recursion vs. loops Functional compilers optimize away tail recursion Where the function ends with return recursive_call( ) For most other systems it doesn t matter write what is most natural If you have to encode the stack with a data structure of your own it probably isn t worth it, the machine stack is faster than yours For inner loops it may make a difference verify with a profiler before optimizing Do the caller and callee make up substantial time? Low-level arithmetic Good compilers can often optimize well Exception: floating-point optimizations may change the result due to round-off errors. EXAMPLE Compilers won t optimize automatically You should only optimize if you know what you are doing 19 Don t assume you know the bottlenecks They may depend on the compiler, hardware, input, etc. You don t know until you measure 20 10

Profiler Dynamic analysis tool How much time is spent in each function Direct and transitive How much memory is used by each data structure 21 How Profilers Work Timing Sampling Periodically interrupt program, look at stack Ascribe 1 time unit to function at top of stack, 1 time unit (transitively) to all other functions on stack Requires running for a while to get averages Measuring Measure time on entry and exit of each function High overhead, requires very accurate clock Memory Periodically, or on demand, capture snapshot of heap Write size and type of each object to file 22 11

Profiling Principles Identify the hot spots Places where the program spends most of its time Make incremental improvements Re-run profiler after each change Keep only the changes that make improvements 23 Profiling Demonstration 24 12

Session Summary Big-O Performance Analysis Determines how an algorithm s run-time scales as the input grows large Optimization Don t optimize prematurely, or without measuring Focus on algorithms, caching, concurrency, hot spots Profiling tools Measure where execution time and memory is spent 25 Further Reading McConnell, Steve. Code Complete, Second Edition, ch. 25. Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. Section 3.1: Asymptotic notation, pp.41 50. 26 13