from time import perf_counter import sys # number of elements in collections SIZE = 25000 def count_numbers(collection): '''repeatedly lookup numbers in a collection''' t_start = perf_counter() # start timing count = 0 for i in range(SIZE): if i in collection: # lookup a number in the collection count += 1 duration = perf_counter() - t_start print('Located {0} numbers'.format(count)) print('Function took {:.3} secs'.format(duration)) # first, use a list nums_list = list(range(SIZE)) print("Running function with list...") count_numbers(nums_list) # print a blank line print() # display output immediately (don't worry about this line) sys.stdout.flush() # second, use a set nums_set = set(range(SIZE)) print("Running function with set...") count_numbers(nums_set)