Skip to content

Instantly share code, notes, and snippets.

@staciax
Last active February 11, 2026 14:36
Show Gist options
  • Select an option

  • Save staciax/26ec8b9a930c60c7811cb829d465bde9 to your computer and use it in GitHub Desktop.

Select an option

Save staciax/26ec8b9a930c60c7811cb829d465bde9 to your computer and use it in GitHub Desktop.
import sys
import timeit
NUMBER_OF_TESTS = 10_000
RANGE_SIZE = 100_000
list_test = timeit.timeit(f'{RANGE_SIZE - 1} in x', setup=f'x = list(range({RANGE_SIZE}))', number=NUMBER_OF_TESTS)
tuple_test = timeit.timeit(f'{RANGE_SIZE - 1} in x', setup=f'x = tuple(range({RANGE_SIZE}))', number=NUMBER_OF_TESTS)
set_test = timeit.timeit(f'{RANGE_SIZE - 1} in x', setup=f'x = set(range({RANGE_SIZE}))', number=NUMBER_OF_TESTS)
frozenset_test = timeit.timeit(f'{RANGE_SIZE - 1} in x', setup=f'x = frozenset(range({RANGE_SIZE}))', number=NUMBER_OF_TESTS)
print('time benchmark (Lower is better)')
print(f'List: {list_test:.4f}s')
print(f'Tuple: {tuple_test:.4f}s')
print(f'Set: {set_test:.4f}s')
print(f'FrozenSet: {frozenset_test:.4f}s')
print('\nmemory benchmark (Lower is better)')
data_range = range(RANGE_SIZE)
x_list = list(data_range)
x_tuple = tuple(data_range)
x_set = set(data_range)
x_frozenset = frozenset(data_range)
print(f'List: {sys.getsizeof(x_list)} bytes')
print(f'Tuple: {sys.getsizeof(x_tuple)} bytes')
print(f'Set: {sys.getsizeof(x_set)} bytes')
print(f'FrozenSet: {sys.getsizeof(x_frozenset)} bytes')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment