Jan 31
Questions about anything from the reading or last week's homework?
Finish going over my notes from last time, including discussing
- the textbook anagram algorithm
- run time for built-in python data structures and their methods
- timing programs (see below)
O() details
There are several similar concepts :
- O(n) is an upper bound (i.e. "at most", i.e. ≤ )
- Ω(n) represents lower bound (i.e. "a least", i.e. ≥ )
- Θ(n) means tight bound (i.e. "approximately", i.e. ≈ ) ;
- f(x) = Θ(g(x)) iff f(x) = O(g(x)) and f(x) = Ω(g(x))
What is the difference between
- worst case runtime
- average runtime (average over what?)
measuring execution speed
count program steps
Put "steps=0" at the top of the program, and "steps+=1" between every line in your code. Then plot steps as a function of n.
Advantages:
- Same algorithm in a given language gives same number of steps on any computer.
- Not dependent on any other activity on the computer.
Disadvantages:
- Awkward to put into the code - can't easily feed a function f(x) into something to count steps.
- Depends on what one of code does, which depends on language features used. (Example: is "a=sorted(b)" one step?)
find elapsed time
Advantages:
- Can do with code outside of function being measured.
- Independent of tricky language features (i.e. "a=sorted(b)).
Disadvantages:
- Will change depending on what else computer is doing - not exactly repeatable.
- Varies substantially with computer power.
time vs space
A common theme is that there is a trade off between memory and execution speed ... we'll see examples of this throughout the term.