Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Comparison of membership check speeds in lists vs sets
- # by @CodingComputing
- # Run this in a Jupyter Notebook
- # CELL 1 (setup stuff)
- import timeit
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
- sizes = 10 ** np.arange(8) # Different container sizes to time
- container_names = ("list", "set")
- # CELL 2 (compare across different sizes)
- size_times_df = pd.DataFrame(columns=container_names)
- # Measure time for membership checks for different sizes
- for size in sizes:
- test_containers = {
- 'list': list(range(size)),
- 'set': set(range(size))
- }
- target = np.floor(size/2) # Choose middle element as target
- for container in test_containers:
- exec_time = timeit.Timer(
- f'{target} in test_containers["{container}"]',
- globals=globals()
- ).timeit(number=10)
- size_times_df.loc[size, container] = exec_time
- #
- size_times_df
- # CELL 3 (graph for Cell 2 results)
- # Plot and customization
- fig, ax = plt.subplots()
- size_times_df.plot(ax=ax, marker='o')
- # Make log scales on axes, because our data points are magnitudes apart
- ax.set_xscale('log')
- ax.set_yscale('log')
- ax.set_xlabel('Container Size', size=12)
- ax.set_ylabel('Membership check time\n(for middle element)', size=12)
- ax.set_title('Comparison of Membership Check Times\nin lists vs sets')
- ax
- # CELL 4 (compare across target positions)
- # Measure membership check times for different positions within the list
- # keeping container size constant
- size = 10_000_000
- positions = [0, 0.25, 0.5, 0.75, 1]
- pos_times_df = pd.DataFrame(columns=container_names)
- #
- for pos in positions:
- target = round(size*pos)
- for container in test_containers:
- exec_time = timeit.Timer(
- f'{target} in test_containers["{container}"]',
- globals=globals()
- ).timeit(number=10)
- pos_times_df.loc[pos, container] = exec_time
- #
- pos_times_df
- # CELL 5 (graph for Cell 4 results)
- fig, ax = plt.subplots()
- pos_times_df.plot(ax=ax, marker='o')
- ax.set_xlabel(f'Relative Position within list\n(Container size: {size})', size=12)
- ax.set_ylabel('Membership check time', size=12)
- ax.set_title('Comparison of Membership Check Times\nin lists vs sets')
- ax
- # That's it! Follow x.com/CodingComputing for more on Python!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement