If compound takes three parameters, why does only one of them change how long it runs, and why does sum_of grow exactly the same way?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
Which parameter costs time can be read off a function's body before anything is measured. For each of the three given functions, find the one parameter that decides how many times its loop runs: for grow one of 'start', 'factor', 'n_steps'; for fill one of 'size', 'rounds', 'value'; for wages one of 'weeks', 'rate', 'hours'. Fill in the dictionary costly, which maps each function's name to that parameter name.
def grow(start, factor, n_steps):
for i in range(n_steps):
start = start * factor
return start
def fill(size, rounds, value):
total = 0
for r in range(rounds):
total = total + size * value
return total
def wages(weeks, rate, hours):
pay = 0
for w in range(weeks):
pay = pay + rate * hours
return payVarying each parameter of compound in turn
Write the three timing experiments (vary the monthly amount, the rate as 1+1/N, then n_months), and record that only n_months moves the runtime, tenfold months giving tenfold time.
Input size of a list is its length
Write out sum_of and the timing loop that builds list(range(N)) for each size, and record that the input size is the list's length, not the magnitude of its elements.
compound and sum_of as algorithmically the same function
Set the two functions' timings side by side, record their similar times and identical tenfold growth, and mark the single loop over the input with its small fixed work in each.
Large inputs versus small-input measurement noise
Record the input size from which the tenfold pattern becomes reliable, and cite the one-element sum_of list timing slower than the ten-element list as an example of small-input noise.