pl pleas ease pl e pleas ease pl e pleas ease v e vot ote
play

Pl Pleas ease, pl e, pleas ease, pl e, pleas ease v e vot - PDF document

Computing In The News Computational Structures in Data Science Bot orders $18,752 of McSundaes every 30 min. to find if machines are working Know before you go... drive-through milkshake style. KA KATE COX OX - 10 10/23/2020, 9:


  1. Computing In The News Computational Structures in Data Science • Bot orders $18,752 of McSundaes every 30 min. to find if machines are working – Know before you go... drive-through milkshake style. – KA KATE COX OX - 10 10/23/2020, 9: 9:49 9 AM UC Berkeley EECS – https://arstechnica.com/information-technology/2020/10/is-mcdonalds-ice-cream- Lecturer machine-working-near-you-theres-a-bot-for-that/ Michael Ball Lecture #18: • Efficiency UC Berkeley | Computer Science 88 | Michael Ball | http://cs88.org UC Berkeley | Computer Science 88 | Michael Ball | http://cs88.org 1 2 Announcements Learning Objectives • Ed post for schedule updates • Runtime Analysis: • Let's just hang out here in one week. – How long will my program take to run? – Why can’t we just use a clock? • Pl Pleas ease, pl e, pleas ease, pl e, pleas ease v e vot ote i e if f – How can we simplify understanding computation in an algorithm • Enjoy this stuff? Take 61B! you ca you can! • Find it challenging? Don’t worry! It’s a different way of thinking. UC Berkeley | Computer Science 88 | Michael Ball | http://cs88.org UC Berkeley | Computer Science 88 | Michael Ball | http://cs88.org 3 4 Efficiency is all about trade-offs Is this code fast? • Running Code: Takes Time, Requires Memory • Most code doesn’t really need to be fast! Computers, even your – More efficient code takes less time or uses less memory phones are already amazingly fast! • Any computation we do, requires both time and "space" on our computer. • Sometimes…it does matter! • Writing efficient code is not obvious – Lots of data – Sometimes it is even convoluted! – Small hardware • But! – Complex processes • We need a framework before we can optimize code • Slow code takes up battery power • Today, we're going to focus on the time component. UC Berkeley | Computer Science 88 | Michael Ball | http://cs88.org UC Berkeley | Computer Science 88 | Michael Ball | http://cs88.org 5 6 1

  2. Runtime analysis problem & solution Runtime: input size & efficiency • Time w/stopwatch, but… • Definition: CS88 – Different computers may have different runtimes. L – Input size: the # of things in the input. – Same computer may have different runtime on the same input. L – e.g. length of a list, the number of iterations in a loop. – Need to implement the algorithm first to run it. L – Running time as a function of input size CS61B – Measures efficiency • Solution : Count the number of “steps” involved, not time! • Important! – Each operation = 1 step – In CS88 we won’t care about the » 1 + 2 is one step efficiency of your solutions! » lst[5] is one step – …in CS61B we will CS61C – When we say “runtime”, we’ll mean # of steps, not time! UC Berkeley | Computer Science 88 | Michael Ball | http://cs88.org UC Berkeley | Computer Science 88 | Michael Ball | http://cs88.org 7 8 Runtime analysis : worst or average case? Runtime analysis: Final abstraction Cubic Quadratic Exponential • Could use avg case • Instead of an exact number of operations we’ll use abstraction – Average running time over a vast # of –Want order of growth, or dominant term inputs • In CS88 we’ll consider • Instead: use worst case –Constant Linear – Consider running time as input grows –Logarithmic –Linear • Why? –Quadratic – Nice to know most time we’d ever spend –Exponential – Worst case happens often • E.g. 10 n 2 + 4 log n + n Logarithmic –…is quadratic – Avg is often ~ worst Constant • Often called “Big O” for "order" Graph of order of growth curves – O(1), O(n) … on log-log plot UC Berkeley | Computer Science 88 | Michael Ball | http://cs88.org UC Berkeley | Computer Science 88 | Michael Ball | http://cs88.org 9 10 Example: Finding a student (by ID) Example: Finding a student (by ID) • Input • Input – Unsorted list of students L – Sorted list of students L – Find student S – Find student S • Output • Output : same • Pseudocode Algorithm – True if S is in L, else False • Worst-case running time as • Pseudocode Algorithm • Worst-case running time as – Start in middle function of the size of L? function of the size of L? – Go through one by one, – If match, report true Constant 1. Constant checking for match. 1. – If exhausted, throw away half of Logarithmic 2. Logarithmic – If match, true L and check again in the middle 2. Linear 3. of remaining part of L Linear 3. – If exhausted L and didn’t find S, Quadratic false 4. – If nobody left, report false Quadratic 4. Exponential 5. Exponential 5. UC Berkeley | Computer Science 88 | Michael Ball | http://cs88.org UC Berkeley | Computer Science 88 | Michael Ball | http://cs88.org 11 12 2

  3. Computational Patterns Comparing Fibonacci • If the number of steps to solve a problem is always the same → Constant time: O(1) def iter_fib(n): • If the number of steps increases similarly for each larger input → Linear Time: O(n) x, y = 0, 1 – Most commonly: for each item for _ in range(n): • If the number of steps increases by some a factor of the input → Quadradic Time: O(n 2 ) x, y = y, x+y – Most commonly: Nested for Loops return x • Two harder cases: – Logarithmic Time: O(log n) » We can double our input with only one more level of work def fib(n): # Recursive » Dividing data in “half” (or thirds, etc) if n < 2: – Exponential Time: O(2 n ) return n » For each bigger input we have 2x the amount of work! return fib(n - 1) + fib(n - 2) » Certain forms of Tree Recursion UC Berkeley | Computer Science 88 | Michael Ball | http://cs88.org UC Berkeley | Computer Science 88 | Michael Ball | http://cs88.org 13 13 14 Tree Recursion What next? • Fib(4) → 9 Calls • Understanding algorithmic complexity helps us know whether something is possible to solve. • Fib(5) → 16 Calls • Gives us a formal reason for understanding why a program might be slow • Fib(6) → 26 Calls • This is only the beginning: • Fib(7) → 43 Calls – We’ve only talked about time complexity, but there is space complexity. • Fib(20) → – In other words: How much memory does my program require? – Often you can trade time for space and vice-versa – Tools like “caching” and “memorization” do this. • If you think this is cool take CS61B! UC Berkeley | Computer Science 88 | Michael Ball | http://cs88.org UC Berkeley | Computer Science 88 | Michael Ball | http://cs88.org 15 15 16 3

Recommend


More recommend