Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 177 Week 12 Recitation Slides Running Time and Performance.

Similar presentations


Presentation on theme: "1 CS 177 Week 12 Recitation Slides Running Time and Performance."— Presentation transcript:

1 1 CS 177 Week 12 Recitation Slides Running Time and Performance

2 2 Announcements Project 5 is due Thursday, April 29

3 3 QUESTIONS???

4 Algorithm Design Get the right answer  Obviously, the most important thing Use a reasonable amount of memory  Not so important because of the rapidly growing memory sizes  But we still only have limited resources, esp. small electronic devices like cell phones have very limited memory Run as quickly as possible  0.1 sec and 0.3 sec make little difference  What about 10 min and 30 min?  10 years and 30 years?

5 Data Structure In early days, algorithm + data structure = program Although not really these days, they are still the most important parts of programs The graph is a typical and extremely useful data structure Actually data structures are everywhere: strings, arrays, linked lists, classes There are more complicated data structures like trees and hash tables which you will learn later

6 O Big Oh analysis is not precise estimation It only gives an idea how slow the program will become when n is larger and larger A simple way to determine the Big Oh value is to check the number of embedded loops for x = 1..n for y = 1..n O(n 2 ) for x = 1..100 for y = 1..100 O(1) for x = 1..n 3 for y = 1..n 0.5 for z = 1..n O(n 4.5 )

7 Sorting In general, sorting algorithms are O(n 2 ) because they typically involve comparing each value to each other value (a loop inside a loop) to get each value into the proper position. (like bubble sort) But the best sorting algorithms are O(n logn). (like quick sort) It makes difference when n is large enough. For example, 1 million.

8 Runtime Analysis It’s also possible to test your program’s performance by running it many times. It’s hard to get the exact Big Oh value (why?) but you can get a curve about how rapidly your program slows down when the data size increases  Because Big Oh is not precise, especially when n is not big enough You can learn more sophisticated ways to do run time analysis in algorithm courses

9 Space Analysis Usually, the memory sizes nowadays are sufficient for you program. However, sometimes, the space cost is extremely large.  What about sorting all bytes in your hard drive? Before you start designing “fast” algorithms, please consider the space cost first Java is convenient for its garbage collector, which saves you a lot of effort by helping clean useless objects, arrays, etc. Sometimes, there’s a tradeoff between time and space.

10 Example for Algorithm Design Search an integer in a sorted array of integers with ascending order Obvious solution: check the integers one by one until you find the wanted one  Time cost: O(n)  Space cost: O(n) Better solution: binary search  Time cost: O(log n) (why?)  Space cost: O(n)

11 11 Final QUESTIONS???


Download ppt "1 CS 177 Week 12 Recitation Slides Running Time and Performance."

Similar presentations


Ads by Google