This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CSE 143 Lecture 8 Complexity slides created by Ethan Apter http://www.cs.washington.edu/143/ 2 Intuition • Are the following operations “fast” or “slow”? behavior fast/slow add at front add at back get at index resizing binary search behavior fast/slow add at front add at back get at index resizing binary search array linked list behavior fast/slow add at front slow add at back fast get at index fast resizing slow binary search (pretty) fast behavior fast/slow add at front fast add at back slow get at index slow resizing N/A (fast!) binary search (really) slow 3 Complexity • “Complexity” is a word that has a special meaning in computer science • complexity : the amount of computational resources a block of code requires in order to run • main computational resources: – time : how long the code takes to execute – space : how much computer memory the code consumes • Often, one of these resources can be traded for the other: – e.g.: we can make some code use less memory if we don’t mind that it will need more time to finish (and viceversa) 4 Time Complexity • We usually care more about time complexity – we want to make our code run fast! • But we don’t merely measure how long a piece of code takes to determine it’s time complexity – Why not? • That approach would have results strongly skewed by: – size/kind of input – speed of the computer’s hardware – other programs running at the same time – operating system – etc 5 Time Complexity • Instead, we care about the growth rate as the input size increase • First, we have to be able to measure the input size – the number of names to sort – the number of nodes in a linked list – the number of students in the IPL queue • We usually call the input size “n” • What happens if we double the input size (n 2n)? – Will the running time double? quadruple? take forever? 6 Time Complexity • We can learn about this growth rate in two ways: – by examining code – by running the same code over different input sizes • Measuring the growth rate by is one of the few places where computer science is like the other sciences – here, we actually collect data • But this data can be misleading – modern computers are very complex – some features (code optimizations) interfere with our data 7 Time Complexity • We’ll count most “simple” statements as 1 time unit – this includes i = i + 1 , int x = elementData[i] , etc – but not loops! (or methods that contain loops!) 8 Time Complexity • Examples: int x = 4 * 10 / 3 + 2  10 * 42; for (int i = 0; i < 100; i++) { x += i; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { x += i + j; } } 1 100 9...
View
Full
Document
This note was uploaded on 09/29/2010 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.
 Spring '08
 SR

Click to edit the document details