Mit6 100l f22 Lec22
Mit6 100l f22 Lec22
1
TIMING
6.100L Lecture 22
TIMING A PROGRAM
6.100L
6.0001Lecture
LECTURE 822
EXAMPLE: convert_to_km, compound
def convert_to_km(m):
return m * 1.609
6.100L
6.0001Lecture
LECTURE 922
CREATING AN INPUT LIST
L_N = [1]
for i in range(7):
L_N.append(L_N[-1]*10)
for N in L_N:
t = time.perf_counter()
km = convert_to_km(N)
dt = time.perf_counter()-t
print(f"convert_to_km({N}) took {dt} seconds ({1/dt}/sec)")
6.100L
6.0001Lecture
LECTURE 922
RUN IT!
convert_to_km OBSERVATIONS
6.100L Lecture 22
MEASURE TIME:
compound with a variable number of months
6.100L
6.0001Lecture
LECTURE 922
MEASURE TIME: sum over L Observation 1: Size of the input is
now the length of the list, not
how big the element numbers are.
def sum_of(L):
total = 0.0 Observation 2: average time
for elt in L: seems to increase by 10 as size of
total = total + elt argument increases by 10
return total
Observation 3: relationship
L_N = [1] between size and time only
for i in range(7): predictable for large sizes
L_N.append(L_N[-1]*10)
Observation 4: Time seems
for N in L_N: comparable to computation of
L = list(range(N)) compound
t = time.perf_counter()
s = sum_of(L)
dt = time.perf_counter()-t
print(f"sum_of({N}) took {dt} seconds ({1/dt}/sec)")
6.100L
6.0001Lecture
LECTURE 922
MEASURE TIME: find element in a list
# search each element one-by-one
def is_in(L, x):
for elt in L:
if elt==x:
return True
return False
6.100L
6.0001Lecture
LECTURE 922
MEASURE TIME: find element in a list
10
9/28/20 6.100L
6.0001Lecture
LECTURE 922
MEASURE TIME: find element in a list
11
9/28/20 6.100L
6.0001Lecture
LECTURE 922
MEASURE TIME: find element in a list
12
9/28/20 6.100L
6.0001Lecture
LECTURE 922
MEASURE TIME: find element in a list
9/28/20 6.100L
6.0001Lecture
LECTURE 922
MEASURE TIME: find element in a list
6.100L
6.0001Lecture
LECTURE 922
MEASURE TIME: diameter function
L=[(cos(0), sin(0)),
(cos(1), sin(1)),
(cos(2), sin(2)), ... ] #example numbers
def diameter(L):
farthest_dist = 0
for i in range(len(L)):
for j in range(i+1, len(L)):
p1 = L[i]
p2 = L[j]
dist = math.sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2)
if dist > farthest_dist:
farthest_dist = dist
return farthest_dist
16
6.100L
6.0001Lecture
LECTURE 922
MEASURE TIME: diameter function
def diameter(L):
farthest_dist = 0
for i in range(len(L)):
for j in range(i+1, len(L)):
p1 = L[i]
p2 = L[j]
dist = math.sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2)
if dist > farthest_dist:
farthest_dist = dist
return farthest_dist
17
6.100L
6.0001Lecture
LECTURE 922
MEASURE TIME: diameter function
def diameter(L):
farthest_dist = 0
for i in range(len(L)):
for j in range(i+1, len(L)):
p1 = L[i]
p2 = L[j]
dist = math.sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2)
if dist > farthest_dist:
farthest_dist = dist
return farthest_dist
18
6.100L
6.0001Lecture
LECTURE 922
MEASURE TIME: diameter function
def diameter(L):
farthest_dist = 0
for i in range(len(L)):
for j in range(i+1, len(L)):
p1 = L[i]
p2 = L[j]
dist = math.sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2)
if dist > farthest_dist:
farthest_dist = dist
return farthest_dist
19
6.100L
6.0001Lecture
LECTURE 922
MEASURE TIME: diameter function
def diameter(L):
farthest_dist = 0
for i in range(len(L)):
for j in range(i+1, len(L)):
p1 = L[i]
p2 = L[j]
dist = math.sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2)
if dist > farthest_dist:
farthest_dist = dist
return farthest_dist
20
6.100L
6.0001Lecture
LECTURE 922
MEASURE TIME: diameter function
def diameter(L):
farthest_dist = 0
for i in range(len(L)):
for j in range(i+1, len(L)):
p1 = L[i]
p2 = L[j]
dist = math.sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2)
if dist > farthest_dist:
farthest_dist = dist
return farthest_dist
21
6.100L
6.0001Lecture
LECTURE 922
MEASURE TIME: diameter function
def diameter(L):
farthest_dist = 0
for i in range(len(L)):
for j in range(i+1, len(L)):
p1 = L[i]
p2 = L[j]
dist = math.sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2)
if dist > farthest_dist:
farthest_dist = dist
return farthest_dist
22
6.100L
6.0001Lecture
LECTURE 922
MEASURE TIME: diameter function
def diameter(L):
farthest_dist = 0
for i in range(len(L)):
for j in range(i+1, len(L)):
p1 = L[i]
p2 = L[j]
dist = math.sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2)
if dist > farthest_dist:
farthest_dist = dist
return farthest_dist
6.100L
6.0001Lecture
LECTURE 922
PLOT OF INPUT SIZE vs. TIME TO RUN
is_in
binary_search
linear logarithmic
diameter
quadratic
24
6.100L Lecture 22
TWO DIFFERENT MACHINES
My old laptop My old desktop
Observation 1: even for the same code, the actual machine may affect speed.
Observation 2: Looking only at the relative increase in run time from a prev run,
if input is n times as big, the run time is approx. n times as long.
25
DON’T GET ME WRONG!
6.100L
6.0001Lecture
LECTURE 922
COUNTING
27
6.100L Lecture 22
COUNT OPERATIONS
convert_to_km 2 ops
Assume these steps take def convert_to_km(m):
constant time: return m * 1.609
• Mathematical operations
• Comparisons
sum_of 1+len(L)*3+1 = 3*len(L)+2 ops
• Assignments def sum_of(L):
• Accessing objects in total = 0
memory for i in L:
Count number of these total += i
operations executed as return total
function of size of input
28
6.100L
6.0001Lecture
LECTURE 822
COUNT OPERATIONS: is_in
29
6.100L
6.0001Lecture
LECTURE 922
COUNT OPERATIONS: is_in
30
6.100L
6.0001Lecture
LECTURE 922
COUNT OPERATIONS:
binary search
6.100L
6.0001Lecture
LECTURE 922
COUNT OPERATIONS
is_in testing
Observation 1: number of
for 1 element, is_in used 9 operations
operations for is_in increases by
for 10 element, is_in used 37 operations
10 as size increases by 10
for 100 element, is_in used 307 operations
for 1000 element, is_in used 3007 operations
for 10000 element, is_in used 30007 operations
for 100000 element, is_in used 300007 operations
for 1000000 element, is_in used 3000007 operations
binary_search testing
for 1 element, binary search used 15 operations Observation 2: but number
for 10 element, binary search used 85 operations of operations for binary
for 100 element, binary search used 148 operations search grows much more
for 1000 element, binary search used 211 operations slowly. Unclear at what rate.
for 10000 element, binary search used 295 operations
for 100000 element, binary search used 358 operations
for 1000000 element, binary search used
32
421 operations
10/5/20 6.100L
6.0001Lecture
LECTURE 922
PLOT OF INPUT SIZE vs. OPERATION COUNT
33
6.100L Lecture 22
PROBLEMS WITH TIMING AND COUNTING
6.100L Lecture 22
EFFICIENCY IN TERMS OF INPUT: BIG-PICTURE
RECALL mysum (one loop) and square (nested loops)
mysum(x)
What happened to the program efficiency as x increased?
10 times bigger x meant the program
Took approx. 10 times as long to run
Did approx. 10 times as many ops
Express it in an “order of” way vs. the input variable: efficiency = Order of x
square(x)
What happened to the program efficiency as x increased?
2 times bigger x meant the program
Took approx. 4 times as long to run
Did approx. 4 times as many ops
10 times bigger x meant the program
Took approx. 100 times as long to run
Did approx. 100 times as many ops
Express it in an “order of” way vs. the input variable: efficiency = Order of x2
35
6.100L Lecture 22
ORDER of GROWTH
36
6.100L Lecture 22
ORDERS OF GROWTH
It’s a notation
Evaluates programs when input is very big
Expresses the growth of program’s run time
Puts an upper bound on growth
Do not need to be precise: “order of” not “exact” growth
37
6.100L Lecture 22
A BETTER WAY
A GENERALIZED WAY WITH APPROXIMATIONS
6.100L Lecture 22
WHICH INPUT TO USE TO MEASURE EFFICIENCY
Could be an integer
-- convert_to_km(x)
Could be length of list
-- list_sum(L)
You decide when multiple parameters to a function
-- is_in(L, e)
Might be different depending on which input you consider
39
6.100L
6.0001Lecture
LECTURE 822
DIFFERENT INPUTS CHANGE HOW
THE PROGRAM RUNS
40
6.100L
6.0001Lecture
LECTURE 822
DIFFERENT INPUTS CHANGE HOW
THE PROGRAM RUNS
41
6.100L
6.0001Lecture
LECTURE 822
DIFFERENT INPUTS CHANGE HOW
THE PROGRAM RUNS
6.100L
6.0001Lecture
LECTURE 822
ASYMPTOTIC GROWTH
6.100L
6.0001Lecture
LECTURE 822
BIG O Definition
3𝑥𝑥 2 + 20𝑥𝑥 + 1 = 𝑂𝑂(𝑥𝑥 2 )
Crossover
f(x) = O(g(x)) means that g(x) times
some constant eventually always
exceeds f(x)
Eventually means above some
threshold value of x
4𝑥𝑥 2 > 3𝑥𝑥 2 + 20𝑥𝑥 + 1∀𝑥𝑥 > 20.04
44
6.100L Lecture 22
BIG O FORMALLY
A big Oh bound is an upper bound on the growth of some function
𝑓𝑓(𝑥𝑥) = 𝑂𝑂(𝒈𝒈(𝒙𝒙)) means there exist
constants 𝒄𝒄𝟎𝟎 , 𝒙𝒙𝟎𝟎 for which 𝒄𝒄𝟎𝟎 𝒈𝒈(𝒙𝒙) ≥ 𝒇𝒇(𝒙𝒙) for all 𝑥𝑥 > 𝒙𝒙𝟎𝟎
Example: 𝑓𝑓(𝑥𝑥) = 3𝑥𝑥 2 + 20𝑥𝑥 + 1
Crossover at
x=20.04 orange > blue
for all x > 20.04)
These lines
will never
cross again
0 <= x <= 30 45
0 <= x <= 100
6.100L Lecture 22
BIG Θ Definition 3𝑥𝑥 2 − 20𝑥𝑥 − 1 = 𝜃𝜃(𝑥𝑥 2 )
A big Θ bound is a lower and upper bound on the growth of some function
Suppose 𝑓𝑓(𝑥𝑥) = 3𝑥𝑥 2 − 20𝑥𝑥 − 1
𝒇𝒇(𝒙𝒙) = Θ(𝒈𝒈(𝒙𝒙)) means:
there exist constants 𝒄𝒄𝟎𝟎 , 𝑥𝑥0 for which 𝒄𝒄𝟎𝟎 𝒈𝒈(𝒙𝒙) ≥ 𝒇𝒇(𝒙𝒙) for all 𝑥𝑥 > 𝒙𝒙𝟎𝟎
and constants 𝒄𝒄𝟏𝟏 , 𝑥𝑥1 for which 𝒄𝒄𝟏𝟏 𝒈𝒈(𝒙𝒙) ≤ 𝒇𝒇(𝒙𝒙) for all 𝑥𝑥 > 𝒙𝒙𝟏𝟏
Example, 𝒇𝒇(𝒙𝒙) = Θ(𝒙𝒙𝟐𝟐 ) because 𝟒𝟒𝒙𝒙𝟐𝟐 > 𝟑𝟑𝒙𝒙𝟐𝟐 − 𝟐𝟐𝟐𝟐𝟐𝟐 − 𝟏𝟏 ∀𝑥𝑥 ≥ 𝟎𝟎 (𝒄𝒄𝟎𝟎 = 𝟒𝟒, 𝒙𝒙𝟎𝟎 = 𝟎𝟎)
and 𝟐𝟐𝒙𝒙𝟐𝟐 < 𝟑𝟑𝒙𝒙𝟐𝟐 − 𝟐𝟐𝟐𝟐𝟐𝟐 − 𝟏𝟏 ∀𝑥𝑥 ≥ 𝟐𝟐𝟐𝟐 (𝒄𝒄𝟏𝟏 = 𝟐𝟐, 𝒙𝒙𝟏𝟏 = 𝟐𝟐𝟐𝟐. 𝟎𝟎𝟎𝟎)
46
𝒇𝒇 𝒙𝒙 = 𝜣𝜣 𝒙𝒙𝟐𝟐
≠ Θ 𝑥𝑥 3 ≠ Θ 2𝑥𝑥 and anything higher order because they
upper bound but not lower bound it
47
6.100L Lecture 22
SIMPLIFICATION EXAMPLES
Θ(n2) : n2 + 2n + 2
Θ(x2) : 3x2 + 100000x + 31000
Θ(a) : log(a) + a + 4
48
6.100L
6.0001Lecture
LECTURE 822
BIG IDEA
Express Theta in terms of
the input.
Don’t just use n all the time!
49
6.100L Lecture 22
YOU TRY IT!
Θ(x) : 1000*log(x) + x
Θ(n3) : n2log(n) + n3
Θ(y) : log(y) + 0.000001y
Θ(2b) : 2b + 1000a2 + 100*b2 + 0.0001a3
Θ(a3)
Θ(2b+a3)
All could be ok, depends on the input we care about
50
6.100L Lecture 22
USING Θ TO EVALUATE YOUR
ALGORITHM
def fact_iter(n):
"""assumes n an int >= 0"""
answer = 1
while n > 1:
answer *= n
n -= 1
return answer
Number of steps: 5n + 2
Worst case asymptotic complexity: Θ(n)
Ignore additive constants
2 doesn’t matter when n is big
Ignore multiplicative constants
5 doesn’t matter if just want to know how increasing n changes time
needed
51
6.100L
6.0001Lecture
LECTURE 822
COMBINING COMPLEXITY CLASSES
LOOPS IN SERIES
Analyze statements inside functions to get order of growth
Apply some rules, focus on dominant term
6.100L
6.0001Lecture
LECTURE 822
COMBINING COMPLEXITY CLASSES
NESTED LOOPS
Analyze statements inside functions to get order of growth
Apply some rules, focus on dominant term
6.100L
6.0001Lecture
LECTURE 822
ANALYZE COMPLEXITY
def f(x):
answer = 1
for i in range(x): Outer loop is Θ(x)
for j in range(i,x): Inner loop is Θ(x)
answer += 2 Everything else is Θ(1)
return answer
54
6.100L Lecture 22
YOU TRY IT!
What is the Theta complexity of this program? Careful to
describe in terms of input
(hint: what matters with a list, size of elems of length?)
def f(L):
Lnew = []
for i in L:
Lnew.append(i**2)
return Lnew
ANSWER:
Loop: Θ(len(L))
f is Θ(len(L))
55
6.100L Lecture 22
YOU TRY IT!
What is the Theta complexity of this program?
def f(L, L1, L2):
""" L, L1, L2 are the same length """
inL1 = False
for i in range(len(L)):
if L[i] == L1[i]:
inL1 = True
inL2 = False
for i in range(len(L)):
if L[i] == L2[i]:
inL2 = True
return inL1 and inL2
ANSWER:
Loop: Θ(len(L)) + Θ(len(L))
f is Θ(len(L)) or Θ(len(L1)) or Θ(len(L2))
56
6.100L Lecture 22
COMPLEXITY CLASSES
6.100L
6.0001Lecture
LECTURE 822
COMPLEXITY GROWTH
6.100L Lecture 22
SUMMARY
6.100L Lecture 22
MITOpenCourseWare
https://ocw.mit.edu
For information about citing these materials or our Terms ofUse,visit: https://ocw.mit.edu/terms.
60