def has_dups(l):
s = frozenset(l)
return len(s) < len(l)
def has_dups_list(l):
for v in l:
count = 0
for v2 in l:
if v == v2:
count += 1
if count > 1:
return True
return False
def timing(func, argument, repeat=100):
import time
total = 0.0
for i in range(repeat):
start = time.time()
func(argument)
total += time.time() - start
print("Average time (ms):", total / repeat * 1000.0)
def check(test_list):
print(has_dups(test_list), has_dups_list(test_list))
timing(has_dups, test_list)
timing(has_dups_list, test_list)
test_list = list(range(1000))
check(test_list)
test_list = list(range(1000)) ; test_list[-1] = test_list[500]
check(test_list)
test_list = list(range(1000)) ; test_list[1] = test_list[0]
check(test_list)