|
2 | 2 |
|
3 | 3 | import matplotlib.pyplot as plt |
4 | 4 |
|
5 | | -from test_cases import build_list |
| 5 | +from test_fixtures import build_list |
6 | 6 |
|
7 | 7 | TIMEIT_TIMES = 100_000 |
8 | 8 | LIST_SIZE = 500 |
9 | 9 | POSITION_INCREMENT = 10 |
10 | 10 |
|
11 | 11 | looping_times = [] |
12 | 12 | generator_times = [] |
| 13 | +in_times = [] |
13 | 14 | positions = [] |
14 | 15 |
|
| 16 | + |
| 17 | +def find_match_loop(list_to_search, item_to_find): |
| 18 | + for val in list_to_search: |
| 19 | + if val == item_to_find: |
| 20 | + return val |
| 21 | + return None |
| 22 | + |
| 23 | + |
| 24 | +def find_match_gen(list_to_search, item_to_find): |
| 25 | + return next((val for val in list_to_search if val == item_to_find), None) |
| 26 | + |
| 27 | + |
| 28 | +def find_match_in(list_to_search, item_to_find): |
| 29 | + if item_to_find in list_to_search: |
| 30 | + return item_to_find |
| 31 | + |
| 32 | + |
15 | 33 | for position in range(0, LIST_SIZE, POSITION_INCREMENT): |
16 | 34 | print( |
17 | 35 | f"Progress {position/LIST_SIZE:.0%}", |
18 | | - end=f"{3 * ' '}\r", # Clear previous characters and reset cursor |
| 36 | + end=f"{3 * ' '}\r", # Clear previous characters and resets cursor |
19 | 37 | ) |
20 | 38 |
|
21 | 39 | positions.append(position) |
|
26 | 44 |
|
27 | 45 | looping_times.append( |
28 | 46 | timeit( |
29 | | - "find_match_loop(list_to_search, 'Real Python', None)", |
30 | | - setup="from looping import find_match_loop", |
| 47 | + "find_match_loop(list_to_search, 'Real Python')", |
31 | 48 | globals=globals(), |
32 | 49 | number=TIMEIT_TIMES, |
33 | 50 | ) |
34 | 51 | ) |
35 | 52 | generator_times.append( |
36 | 53 | timeit( |
37 | | - "find_match_gen(list_to_search, 'Real Python', None)", |
38 | | - setup="from generator import find_match_gen", |
| 54 | + "find_match_gen(list_to_search, 'Real Python')", |
| 55 | + globals=globals(), |
| 56 | + number=TIMEIT_TIMES, |
| 57 | + ) |
| 58 | + ) |
| 59 | + in_times.append( |
| 60 | + timeit( |
| 61 | + "find_match_in(list_to_search, 'Real Python')", |
39 | 62 | globals=globals(), |
40 | 63 | number=TIMEIT_TIMES, |
41 | 64 | ) |
|
47 | 70 |
|
48 | 71 | plot = ax.plot(positions, looping_times, label="loop") |
49 | 72 | plot = ax.plot(positions, generator_times, label="generator") |
| 73 | +plot = ax.plot(positions, in_times, label="in") |
50 | 74 |
|
51 | 75 | plt.xlim([0, LIST_SIZE]) |
52 | 76 | plt.xlabel("Position of element to be found") |
|
0 commit comments