|
| 1 | +""" |
| 2 | +Script to chart out the performance of generators, loops and the in operator |
| 3 | +when trying to find the first matching element in a list. |
| 4 | +""" |
| 5 | + |
| 6 | +from timeit import timeit |
| 7 | + |
| 8 | +import matplotlib.pyplot as plt |
| 9 | + |
| 10 | +TIMEIT_TIMES = 100 # Increase number for smoother lines |
| 11 | +LIST_SIZE = 1000 |
| 12 | +POSITION_INCREMENT = 10 |
| 13 | + |
| 14 | +looping_times = [] |
| 15 | +generator_times = [] |
| 16 | +in_times = [] |
| 17 | +positions = [] |
| 18 | + |
| 19 | + |
| 20 | +def build_list(size, fill, value, at_position): |
| 21 | + return [value if i == at_position else fill for i in range(size)] |
| 22 | + |
| 23 | + |
| 24 | +def find_match_loop(list_to_search, item_to_find): |
| 25 | + for value in list_to_search: |
| 26 | + if value == item_to_find: |
| 27 | + return value |
| 28 | + return None |
| 29 | + |
| 30 | + |
| 31 | +def find_match_gen(list_to_search, item_to_find): |
| 32 | + return next( |
| 33 | + (value for value in list_to_search if value == item_to_find), None |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def find_match_in(list_to_search, item_to_find): |
| 38 | + if item_to_find in list_to_search: |
| 39 | + return item_to_find |
| 40 | + |
| 41 | + |
| 42 | +for position in range(0, LIST_SIZE, POSITION_INCREMENT): |
| 43 | + print( |
| 44 | + f"Progress {position / LIST_SIZE:.0%}", |
| 45 | + end=f"{3 * ' '}\r", # Clear previous characters and resets cursor |
| 46 | + ) |
| 47 | + |
| 48 | + positions.append(position) |
| 49 | + |
| 50 | + list_to_search = build_list( |
| 51 | + LIST_SIZE, "Fake Python", "Real Python", position |
| 52 | + ) |
| 53 | + |
| 54 | + looping_times.append( |
| 55 | + timeit( |
| 56 | + "find_match_loop(list_to_search, 'Real Python')", |
| 57 | + globals=globals(), |
| 58 | + number=TIMEIT_TIMES, |
| 59 | + ) |
| 60 | + ) |
| 61 | + generator_times.append( |
| 62 | + timeit( |
| 63 | + "find_match_gen(list_to_search, 'Real Python')", |
| 64 | + globals=globals(), |
| 65 | + number=TIMEIT_TIMES, |
| 66 | + ) |
| 67 | + ) |
| 68 | + in_times.append( |
| 69 | + timeit( |
| 70 | + "find_match_in(list_to_search, 'Real Python')", |
| 71 | + globals=globals(), |
| 72 | + number=TIMEIT_TIMES, |
| 73 | + ) |
| 74 | + ) |
| 75 | + |
| 76 | +print("Progress 100%") |
| 77 | + |
| 78 | +fig, ax = plt.subplots() |
| 79 | + |
| 80 | +plot = ax.plot(positions, looping_times, label="loop") |
| 81 | +plot = ax.plot(positions, generator_times, label="generator") |
| 82 | +plot = ax.plot(positions, in_times, label="in") |
| 83 | + |
| 84 | +plt.xlim([0, LIST_SIZE]) |
| 85 | +plt.ylim([0, max(max(looping_times), max(generator_times), max(in_times))]) |
| 86 | + |
| 87 | +plt.xlabel("Index of element to be found") |
| 88 | +plt.ylabel(f"Time in seconds to find element {TIMEIT_TIMES:,} times") |
| 89 | +plt.title("Raw Time to Find First Match") |
| 90 | +plt.legend() |
| 91 | + |
| 92 | +plt.show() |
| 93 | + |
| 94 | +# Ratio |
| 95 | + |
| 96 | +looping_ratio = [loop / loop for loop in looping_times] |
| 97 | +generator_ratio = [ |
| 98 | + gen / loop for gen, loop in zip(generator_times, looping_times) |
| 99 | +] |
| 100 | +in_ratio = [in_ / loop for in_, loop in zip(in_times, looping_times)] |
| 101 | + |
| 102 | +fig, ax = plt.subplots() |
| 103 | + |
| 104 | +plot = ax.plot(positions, looping_ratio, label="loop") |
| 105 | +plot = ax.plot(positions, generator_ratio, label="generator") |
| 106 | +plot = ax.plot(positions, in_ratio, label="in") |
| 107 | + |
| 108 | +plt.xlim([0, LIST_SIZE]) |
| 109 | +plt.ylim([0, max(max(looping_ratio), max(generator_ratio), max(in_ratio))]) |
| 110 | +plt.xlabel("Index of element to be found") |
| 111 | +plt.ylabel("Speed to find element, relative to loop") |
| 112 | +plt.title("Relative Speed to Find First Match") |
| 113 | +plt.legend() |
| 114 | + |
| 115 | +plt.show() |
0 commit comments