Skip to content

Instantly share code, notes, and snippets.

@JimHaughwout
Created July 3, 2016 15:21
Show Gist options
  • Save JimHaughwout/24e102c13e6c972517a0658233ce4809 to your computer and use it in GitHub Desktop.
Save JimHaughwout/24e102c13e6c972517a0658233ce4809 to your computer and use it in GitHub Desktop.
any vs. all vs. iteration with break
from datetime import datetime
from random import randint
data = list()
for i in range(100000):
j = randint(1,20)
if j == 20:
data.append(j)
else:
data.append(datetime(2016, 6, j, 12, j+1, j*2))
s = datetime.utcnow()
print all(isinstance(item, datetime) for item in data)
e = datetime.utcnow()
print (e-s).total_seconds()*1000
s = datetime.utcnow()
print any(not isinstance(item, datetime) for item in data)
e = datetime.utcnow()
print (e-s).total_seconds()*1000
s = datetime.utcnow()
for item in data:
if not isinstance(item, datetime):
print False
break
e = datetime.utcnow()
print (e-s).total_seconds()*1000
"""
python isAll.py
False
0.876
True
0.018
False
0.036
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment