1 / 29

Announcements

Announcements. You survived midterm 2! No Class / No Office hours Friday. Its all about speed!. … well usually, sometimes its about memory Portable devices, low power devices Today we will focus on: What makes computers fast/slow How to reason about algorithms

Download Presentation

Announcements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Announcements • You survived midterm 2! • No Class / No Office hours Friday

  2. Its all about speed! • … well usually, sometimes its about memory • Portable devices, low power devices • Today we will focus on: • What makes computers fast/slow • How to reason about algorithms • We have seen many ways to do a particular task • How do we choose which way we should write our program?

  3. Big speed differences • Many of the techniques we’ve learned take no time at all in other applications • Select a figure in Word. • It’s automatically inverted as fast as you can click. • Color changes in Photoshop happen as you change the slider • Increase or decrease red? Play with it and see it happen live.

  4. What makes my program fast? • Is it that Photoshop is so fast? • Or that Pythonis so slow? • It’s some of both—it’s not a simple problem with an obvious answer. • We’ll consider two issues: • How fast can computers get • What’s not computable, no matter how fast you go

  5. What a computer really understands • Computers really do not understand Python, nor Java, nor any other language. • The basic computer only understands one kind of language: machinelanguage. • Machine language consists of instructions to the computer expressed in terms of values in bytes. • These instructions tell the computer to do very low-level activities.

  6. Machine language trips the right switches • The computer doesn’t really understand machine language. • The computer is just a machine, with lots of switches that make data flow this way or that way. • Machine language is just a bunch of switch settings that cause the computer to do a bunch of other switch settings. • We interpret those switches to be addition, subtraction, loading, and storing. • In the end, it’s all about encoding. A byte of switches

  7. Assembler and machine language • Machine language looks just like a bunch of numbers. • Assembler language is a set of words that corresponds to the machine language. • It’s often one-to-one relationship. • A word of assembler equals one machine language instruction, typically. • (Often, just a single byte.)

  8. Assembler instructions • Assembler instructions tell the computer to do things like: • Store numbers into particular memory locations or into special locations (variables) in the computer. • Test numbers for equality, greater-than, or less-than. • Add numbers together, or subtract them.

  9. An example assembly language program LOAD 10,R0 ; Load special variable R0 with 10 LOAD 12,R1 ; Load special variable R1 with 12 SUM R0,R1 ; Add R0 and R1, Put the result in R1 STOR R1,#45 ; Store the result into memory location #45 Recall that we talked about memory as a long series of boxes. Each one has a location (number) (like #45). The “special variables” are often called “registers”. That is why they begin with the letter “R”.

  10. Assembler -> Machine LOAD 10,R0 ; Load special variable R0 with 10 LOAD 12,R1 ; Load special variable R1 with 12 SUM R0,R1 ; Add R0 and R1, Put the result in R1 STOR R1,#45 ; Store the result into memory location #45 Might appear in memory as just 12 bytes: 01 00 10 01 01 12 02 00 01 03 01 45

  11. Machine language is executed very quickly • Imagine a relatively slow computer today (not latest generation) having a clock rate of 1.5 Gigahertz. • What that means exactly is hard to explain,but let’s interpret it as processing 1.5 billion bytes per second. • Those 12 bytes would execute inside the computer, then, in 12/1,500,000,000th of a second!

  12. Other factors affect speed • Processor Speed • Cache located on the processor • Memory speed (and size!) • Hard Drive speed • Operating System efficiency

  13. Hard Drive • Slowest form of storage • Unlike RAM, Hard Drive storage is “permanent” • It survives after you turn the power off

  14. Storage relationships • If you have too little RAM, your computer will store some things on hard disk. • It will be slower to bring back into RAM for the computer to use it. • The system bus describes how fast things can move around your computer. • Network is even slower than the hard drive • If you’re grabbing a web page from the network, onto the hard drive, and into RAM, the network speed will be the limiting factor.

  15. How do we compare algorithms? • There’s more than one way to search. • How do we compare algorithms to say that one is faster than another? • Computer scientists use something called Big-O notation • It’s the order of magnitude of the algorithm • Big-O notation tries to ignore differences between languages, even between compiled vs. interpreted, and focus on the number of steps to be executed.

  16. What question are we trying to answer? • If I am given a larger problem to solve (more data), how is the performance of the algorithm affected? • If I change the input, how does the running time change?

  17. We want to choose the algorithm with the best complexity • We want an algorithm which will be fast • A “lower” complexity mean an algorithm will perform better on large input

  18. Finding something in the phone book • O(n) algorithm • Start from the beginning. • Check each page, until you find what you want. • Not very efficient • Best case: One step • Worse case: n steps where n = total entries in phone book • Average case: n/2 steps

  19. Implementing a Linear Search – slightly different deffindInList(something, alist): status = False for item in alist: if (item == something): status = True return status

  20. Running our Search Algorithm >>> print(findInList("bear",["apple","bear","cat","dog","elephant"])) True >>> print(findInList("giraffe",["apple","bear","cat","dog","elephant"])) False

  21. Why is it linear? • Step 1) What is the input to the algorithm? • Well the function takes a list and an element we are looking for • Step 2) How much work are we doing? • We compare each element in the list to the element we are looking for • Lets assume we can test equality in one “step” or one “unit of work”

  22. Implementing a Linear Search deffindInList(something, alist): status = False for item in alist:  how much work we perform if (item == something):  piece of work status = True return status

  23. Implementing a Linear Search – slightly better deffindInList(something, alist): for item in alist: if (item == something): return True return False

  24. So why do we bother with linear search? • Our data might not be ordered! • Notice that our faster algorithms required our data to be ordered • We say that ordered data is sorted • Challenge question: Is it faster to linearly search through unordered data, or to sort it, and then use a binary search? • We will answer this next week!

  25. A better search in a sorted list • O(log n) (log2 n = x where 2x=n) • Split the phone book in the middle. • Is the name you’re at before or after the page you’re looking at? • If after, look from the middle to the end. • If before, look from the start to the middle. • Keep repeating until done • More efficient: • Best case: It’s the first place you look. • Average and worst case: log n steps

  26. Implementing a binary search def findInSortedList(something , alist ): start = 0 end = len(alist) – 1 status = False while start <= end: checkpoint = int(( start+end )/2.0) if alist[checkpoint ]== something: return True if alist[checkpoint]<something: start=checkpoint +1 if alist[checkpoint]>something: end=checkpoint -1 return status

  27. Running the binary search print("Checking at: "+str(checkpoint )+" Start:"+str(start )+" End:"+str(end)) >>> findInSortedList("giraffe" ,["apple","bear","cat”,"dog"]) Checking at: 1 Start :0 End:3 Checking at: 2 Start :2 End:3 Checking at: 3 Start :3 End:3 False >>> findInSortedList("apple” ["apple","bear","cat”,"dog"]) Checking at: 1 Start :0 End:3 Checking at: 0 Start :0 End:0 True >>> findInSortedList("dog" ,["apple","bear","cat”,"dog"]) Checking at: 1 Start :0 End:3 Checking at: 2 Start :2 End:3 Checking at: 3 Start :3 End:3 True

  28. Clicker Question • Did you come to class today? • A) yes • B) no

  29. Homework • Read Chapter 9

More Related