|
| 1 | +import timeit |
| 2 | + |
| 3 | +setup = """\ |
| 4 | +gc.enable() |
| 5 | +locations = {0: "You are sitting in front of a computer learning Python", |
| 6 | + 1: "You are standing at the end of a road before a small brick building", |
| 7 | + 2: "You are at the top of a hill", |
| 8 | + 3: "You are inside a building, a well house for a small stream", |
| 9 | + 4: "You are in a valley beside a stream", |
| 10 | + 5: "You are in the forest"} |
| 11 | +
|
| 12 | +exits = {0: {"Q": 0}, |
| 13 | + 1: {"W": 2, "E": 3, "N": 5, "S": 4, "Q": 0}, |
| 14 | + 2: {"N": 5, "Q": 0}, |
| 15 | + 3: {"W": 1, "Q": 0}, |
| 16 | + 4: {"N": 1, "W": 2, "Q": 0}, |
| 17 | + 5: {"W": 2, "S": 1, "Q": 0}} |
| 18 | +""" |
| 19 | +locations = {0: "You are sitting in front of a computer learning Python", |
| 20 | + 1: "You are standing at the end of a road before a small brick building", |
| 21 | + 2: "You are at the top of a hill", |
| 22 | + 3: "You are inside a building, a well house for a small stream", |
| 23 | + 4: "You are in a valley beside a stream", |
| 24 | + 5: "You are in the forest"} |
| 25 | + |
| 26 | +exits = {0: {"Q": 0}, |
| 27 | + 1: {"W": 2, "E": 3, "N": 5, "S": 4, "Q": 0}, |
| 28 | + 2: {"N": 5, "Q": 0}, |
| 29 | + 3: {"W": 1, "Q": 0}, |
| 30 | + 4: {"N": 1, "W": 2, "Q": 0}, |
| 31 | + 5: {"W": 2, "S": 1, "Q": 0}} |
| 32 | + |
| 33 | + |
| 34 | +def nested_loop(): |
| 35 | + result = [] |
| 36 | + for loc in sorted(locations): |
| 37 | + exits_to_destination_1 = [] |
| 38 | + for ext in exits: |
| 39 | + if loc in exits[ext].values(): |
| 40 | + exits_to_destination_1.append((ext, locations[ext])) |
| 41 | + result.append(exits_to_destination_1) |
| 42 | + # print the result before returning |
| 43 | + for x in result: |
| 44 | + pass |
| 45 | + return result |
| 46 | + |
| 47 | + |
| 48 | +print("---") |
| 49 | + |
| 50 | + |
| 51 | +def loop_comp(): |
| 52 | + result = [] |
| 53 | + for loc in sorted(locations): |
| 54 | + exits_to_destination_2 = [(ext, locations[ext]) for ext in exits if loc in exits[ext].values()] |
| 55 | + result.append(exits_to_destination_2) |
| 56 | + # print the result before returning |
| 57 | + for x in result: |
| 58 | + pass |
| 59 | + return result |
| 60 | + |
| 61 | + |
| 62 | +print("---") |
| 63 | + |
| 64 | + |
| 65 | +def nested_comp(): |
| 66 | + exits_to_destination_3 = [[(ext, locations[ext]) for ext in exits if loc in exits[ext].values()] |
| 67 | + for loc in sorted(locations)] |
| 68 | + # print the result before returning |
| 69 | + for x in exits_to_destination_3: |
| 70 | + pass |
| 71 | + return exits_to_destination_3 |
| 72 | + |
| 73 | + |
| 74 | +def nested_gen(): |
| 75 | + exits_to_destination_3 = ([(ext, locations[ext]) for ext in exits if loc in exits[ext].values()] |
| 76 | + for loc in sorted(locations)) |
| 77 | + # print the result before returning |
| 78 | + for x in exits_to_destination_3: |
| 79 | + pass |
| 80 | + return exits_to_destination_3 |
| 81 | + |
| 82 | + |
| 83 | +# print(nested_loop()) |
| 84 | +# print(loop_comp()) |
| 85 | +# print(nested_comp()) |
| 86 | + |
| 87 | +result_1 = timeit.timeit(nested_loop, setup, number=100000) |
| 88 | +result_2 = timeit.timeit(loop_comp, setup, number=100000) |
| 89 | +result_3 = timeit.timeit(nested_comp, setup, number=100000) |
| 90 | +result_4 = timeit.timeit(nested_gen, setup, number=100000) |
| 91 | +print(f"Nested loop:\t{result_1}") |
| 92 | +print(f"Loop and comp:\t{result_2}") |
| 93 | +print(f"Nested comp:\t{result_3}") |
| 94 | +print(f"Nested gen:\t\t{result_4}") |
0 commit comments