[Exercise] Timing

Knowing the runtime of an algorithm can be analyzed through math. An easy way to test and verify this math is to simulate a bunch of operations and record the time it takes.

For this, we need the time module.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import time

def run():
    start = time.time()

    ### do some stuff

    end = time.time()

    total = end - start

    print("Start time: {}".format(start))
    print("End time: {}".format(end))
    print("Total time: {}".format(total))

run()

Fill in the code above and time the following operations

Looping with squaring:

1
2
3
4
5
6
7
def run():

    x = 0
    for i in range(1000):
        x += i ** 2

run()

Looping with list appending and lookup

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def run():

    unique_list = list()
    repeated_list = list()

    for i in range(1000):
        if i in unique_list:
            repeated_list.append(i)
        else:
            unique_list.append(i)
            unique_list.append(i**2+i)
            unique_list.append(i**2+2*i)

run()

Advanced

You can write a function which takes the run and times it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import time


def time_it(func):

    start = time.time()

    func()

    end = time.time()

    total = end - start

    print("Start time: {}".format(start))
    print("End time: {}".format(end))
    print("Total time: {}".format(total))


def run0():
    x = 0
    for i in range(1000):
        x += i ** 2

def run1():

    unique_list = list()
    repeated_list = list()

    for i in range(1000):
        if i in unique_list:
            repeated_list.append(i)
        else:
            unique_list.append(i)
            unique_list.append(i**2+i)
            unique_list.append(i**2+2*i)


def run_all():
    print("going to run v0 now")

    time_it(run0)

    print("going to run v1 now")

    time_it(run1)

run_all()