import random def methodFour(someList, item): for i in range(len(someList)): if someList[i] == item: return i return -1 from timeit import Timer import random """ #Test time for method four #For this purpose we are going to search for -1 which is NOT found #This demonstrates worst case scenario print("methodFour() should be O(n)") t1 = Timer("methodFour(data,-1)", "from __main__ import methodFour,data") for size in [1000,2000,10000]: data = list(range(size)) print(f"methodFour({size}) : {t1.timeit(number=1000):15.2f} milliseconds") print() """ def methodFourWrapper(size,mode): if mode==-1: search_for=-1 else: search_for = random.randint(0,size*mode) methodFour(list(range(size)),search_for) #Test time for method four print("methodFour() should be O(n)") t1 = Timer("methodFourWrapper(size,mode)", "from __main__ import methodFourWrapper,size,mode") print("methodFour() when the item is never found") mode = -1 for size in [1000,2000,10000]: print(f"{size:6} : {t1.timeit(number=1000):15.2f} milliseconds") print() print("methodFour() when the item is always found") mode = 1 for size in [1000,2000,10000]: print(f"{size:6} : {t1.timeit(number=1000):15.2f} milliseconds") print() print("methodFour() when the item is sometimes found") mode=2 for size in [1000,2000,10000]: print(f"{size:6} : {t1.timeit(number=1000):15.2f} milliseconds") print()