Created
October 31, 2017 18:57
-
-
Save jamesmcnamara/84993fbd2f4ee5f46687ee438b4727a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from timeit import timeit | |
random_list = ''' | |
from random import randint | |
l = [randint(0, 1000) for _ in range(1000000)] | |
value = 50 | |
''' | |
loop = ''' | |
def loop(l): | |
for a in l: | |
if a == value: | |
return True | |
''' | |
contain = ''' | |
def contain(l): | |
return value in l | |
''' | |
set_contain = ''' | |
def set_contain(l): | |
return value in set(l) | |
''' | |
print('loop', timeit('loop(l)', number=50, setup=random_list + loop)) | |
print('contain', timeit('contain(l)', number=50, setup=random_list + contain)) | |
print('set', timeit('set_contain(l)', number=50, setup=random_list + set_contain)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment