The Importance of Runtime Cost! Measuring Runtime Cost! How to Measure Runtime Cost?!

Similar documents
Choice of C++ as Language

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

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

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

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

Lecture 5: Running Time Evaluation

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

Algorithms A Look At Efficiency

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

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

Asymptotic Analysis of Algorithms

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

CS171:Introduction to Computer Science II. Algorithm Analysis. Li Xiong

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

Sum this up for me. Let s write a method to calculate the sum from 1 to some n. Gauss also has a way of solving this. Which one is more efficient?

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

Algorithm Analysis. CENG 707 Data Structures and Algorithms

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

Data Structures Lecture 3 Order Notation and Recursion

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

CS 310: Order Notation (aka Big-O and friends)

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

Elementary maths for GMT. Algorithm analysis Part I

What is an algorithm?

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

Organisation. Assessment

EE 368. Week 6 (Notes)

Analysis of Algorithms Part I: Analyzing a pseudo-code

Introduction to the Analysis of Algorithms. Algorithm

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

Analysis of Algorithm. Chapter 2

csci 210: Data Structures Program Analysis

Analysis of Algorithms

Algorithm Efficiency, Big O Notation, and Javadoc

ALGORITHM ANALYSIS. cs2420 Introduction to Algorithms and Data Structures Spring 2015

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


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

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

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

CS240 Fall Mike Lam, Professor. Algorithm Analysis

O(n): printing a list of n items to the screen, looking at each item once.

Data Structures Lecture 8

(Refer Slide Time: 1:27)

Algorithm Analysis. Performance Factors

Chapter 2: Complexity Analysis

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY

3. Java - Language Constructs I

EECS 203 Spring 2016 Lecture 8 Page 1 of 6

What is an Algorithm?

Algorithm Analysis. Big Oh

Discrete Structures Lecture 15

Scientific Computing. Algorithm Analysis

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

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

Lecture 26. Introduction to Trees. Trees

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

ANALYSIS OF ALGORITHMS

Analysis of Algorithms. CS 1037a Topic 13

Building Java Programs

Algorithmic Analysis. Go go Big O(h)!

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

Lecture Notes on Sorting

Turbo Charge CPU Utilization in Fork/Join Using the ManagedBlocker

Measuring algorithm efficiency

UNIT 1 ANALYSIS OF ALGORITHMS

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

EECS 477: Introduction to algorithms. Lecture 6

6. Asymptotics: The Big-O and Other Notations

Algorithm Analysis. Applied Algorithmics COMP526. Algorithm Analysis. Algorithm Analysis via experiments

Algorithm Analysis. Jordi Cortadella and Jordi Petit Department of Computer Science

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

Principles of Algorithm Analysis. Biostatistics 615/815

Complexity, General. Standard approach: count the number of primitive operations executed.

Computational Complexity Analysis

Introduction to Computer Science

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

Algorithm Analysis. Review of Terminology Some Consequences of Growth Rates. The book s big example: Detecting anagrams. There be math here!

We will use the following code as an example throughout the next two topics:

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Introduction to Computer Science

Data structure and algorithm in Python

Recitation 9. Prelim Review

Intro to Algorithms. Professor Kevin Gold

Big-O-ology 1 CIS 675: Algorithms January 14, 2019

Programming II (CS300)

UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA C ASSIGNMENTS

Suppose that the following is from a correct C++ program:

There are three questions on this exam. You have 2 hours to complete it. Please indent your program so that it is easy for the grader to read.

CSCI 261 Computer Science II

TREES AND ORDERS OF GROWTH 7

How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Section 05: Solutions

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

Section 05: Solutions

ASYMPTOTIC COMPLEXITY

Lecture 2: Algorithm Analysis

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Transcription:

The Importance of Runtime Cost Amazon claims that just an extra one tenth of a second on their response times will cost them 1% in sales" [http://highscalability.com/latency-everywhere-and-it-costs-you-sales-how-crush-it] Lecture 2: Algorithm Analysis (Review of Some 203 Material) Google said they noticed that just a half a second increase in latency caused traffic to drop by a fifth" [http://glinden.blogspot.com/2006/11/marissa-mayer-at-web-20.html]" Measuring Runtime Cost How do you measure runtime cost? Assume can search" 10 names/ms An 8-year old may take longer Population (size) Linear Binary EECS281 (230) 23 ms 0.8 ms UM (40 000) 4 secs 1.5 ms MI (9 mil) 15 mins 2.3 ms Shanghai (23 mil) 38 mins 2.4 ms US (311 mil) 8 hours 38 mins 2.8 ms China (1.3 bil) 1 day 13 hours 3.0 ms World (6.9 bil) 8 days 3.3 ms How to Measure Runtime Cost? Using wall-clock time to measure runtime cost can be tricky: depends on cpu speed depends on cpu load() (but we ll come back to this later) Note: runtime cost == runtime complexity" == runtime == complexity" == time complexity

How to Measure Runtime Cost? Instead we use operation count to measure runtime cost: pick one operation that is performed most often (e.g., ADD, SUB, MUL, DIV, CMP, LOAD, or STORE) count the number of times that operation is performed Roofing Problem s Runtime Cost A builder can carry two tiles on his shoulder as he climbs up the ladder" He then climbs down and carries two more tiles up the ladder" Each round trip (up and down the ladder) costs $2 What is the cost to build a shed (8 tiles)?" 2*ceil(8/2)=8 What is the cost to build a gazebo (128 tiles)?" 2*ceil(128/2)=128 What is the cost to build a house (2048 tiles)?" 2*ceil(2048/2)=2048 What is the ONE operation we counted in this case? Operation Count: findmax() Write a function that given an array of N elements finds the index of the largest element Example: int findmax(int *a, int N) given int a=[3 8 2 7 9 1 5] findmax(a, 7) = 4 Operation Count: findmax() What is the fixed cost of the algorithm? Which operation should we count to compute the variable cost? How many times is this operation executed in the worst case?

A More Complicated Function void f(int N, int *p) // p an array N integers { int i, j, k, l; i = N*2; j = i+1; k = i-3*400*j; l = k/2; How do we go about computing the time complexity of this function? if (l < 0) { i = N+j; } else { i = log2(k); } for (i = 0; i < N; i++) { j = p[i]*3; } for (i = 0; i < N; i++) { for (k = i; k < N; k++) { j = p[j]*3; } } return; } Four Accounting Rules Rule 1: Consecutive Statements: S 1 ; S 2 ; S 3 ; ; S N ; The runtime R of a sequence of statements is the runtime of the statement with the max runtime:" max(r(s 1 ), R(S 2 ),, R(S N )) i = N*2; j = i+1; k = i-3*400*j; l = k/2; Rule 2: Conditional Execution: if (S 1 ) S 2 ; else S 3 ; The runtime of a conditional execution is" max(r(s 1 ), R(S 2 ), R(S 3 )) if (l < 0) { i = N+j; } else { i = log2(k); } Four Accounting Rules Rule 3: Iteration/Loop: for (S 1 ; S 2 ; S 3 ) S 4 ; The runtime of a loop with N iterations is" max(r(s 1 ), N*R(S 2 ), N*R(S 3 ), N*R(S 4 )) for (i = 0; i < N; i++) { j = p[i]*3; } Rule 4: Nested Loop: for (S 1 ; S 2 ; S 3 ) { for (S 4 ; S 5 ; S 6 ) { S 7 ; } } Count inside out: the" for (i = 0; i < N; i++) { runtime is the max of the" for (k = i; k < N; k++) { runtime of each" j = p[j]*3; statement multiplied" } by the total number of" } times each statement is executed A More Complicated Function void f(int N, int *p) // p an array N integers { int i, j, k, l; i = N*2; j = i+1; k = i-3*400*j; l = k/2; What is the time complexity of this function? if (l < 0) { i = N+j; } else { i = log2(k); } for (i = 0; i < N; i++) { j = p[i]*3; } for (i = 0; i < N; i++) { for (k = i; k < N; k++) { j = p[j]*3; } } return; }

Aside: Arithmetic Series What is the sum of all integers from: 0 to 4 inclusive? 1 to 25 inclusive? 3 to 12 inclusive? Operation Count: computerank() Write a function that given an unsorted array a of N elements computes an array r, where r[i] is the rank of a[i] in a The rank of an element is: the number of elements in the array that is smaller than it, plus the number of elements to its left that is equal to it Example: void computerank(int *a, int *r, int N) given int a=[4 3 9 3 7] results in r=[2 0 4 1 3] What Affects Runtime? The algorithm Implementation details skills of the programmer Compiler (and options used) g++ -g (for debugging) g++ -03 (for speed) CPU/memory speed and workload (other programs running concurrently) Amount of data processed (N = input size) Runtime Scalability If N (problem size) is small, longish runtime is tolerable, for large N (N > 10 4, or N in general), only algorithms with low time complexity are usable Scalability: an algorithm/data structure is not scalable if its complexity grows so fast that it requires more resources than available for the expected input size More generally: an algorithm/data structure is not scalable if its complexity grows faster than the rate of increase in input size

Runtime vs. Input Size Rate of growth of runtime depends mostly on input size, independent of most other factors CPU speed, compiler, etc. In algorithm analysis," we re usually only" interest in large N," larger than some n 0 runtime (R) g(n) = N 2 f(n) = N To ease discussion," we usually compare" the complexity of" n 0 = 1 input size (N) our algorithm against" some well-known landmark functions Landmark Functions constant: 1 logarithmic: log N square root: "N linear: N loglinear: N log N quadratic: N 2 cubic: N 3 in general, polynomial: N k, k " 1 exponential: a N, a > 1," usually a = 2 factorial: N