Skip to content

Instantly share code, notes, and snippets.

@bdbaddog
Last active June 14, 2019 15:21
Show Gist options
  • Save bdbaddog/613feb47c06ac8361286f6e077f93706 to your computer and use it in GitHub Desktop.
Save bdbaddog/613feb47c06ac8361286f6e077f93706 to your computer and use it in GitHub Desktop.
Williams-MacBook-Pro-2:tmp bdbaddog$ python bill_try_dict_keyerror.py
hiit: 0.04
hii : 0.03
nit : 0.82
nii : 0.07
Williams-MacBook-Pro-2:tmp bdbaddog$ python3.7 bill_try_dict_keyerror.py
hiit: 0.04
hii : 0.03
nit : 0.41
nii : 0.03
Williams-MacBook-Pro-2:tmp bdbaddog$ python3.6 bill_try_dict_keyerror.py
hiit: 0.03
hii : 0.03
nit : 0.41
nii : 0.04
Williams-MacBook-Pro-2:tmp bdbaddog$ python3.5 bill_try_dict_keyerror.py
hiit: 0.03
hii : 0.03
nit : 0.31
nii : 0.03
from __future__ import print_function
import timeit
setup="""\
test_dict= {'haveit':99}
"""
hasit_try = """\
try:
test_dict['haveit']
except KeyError:
pass
"""
notit_try = """\
try:
test_dict['donthaveit']
except KeyError:
pass
"""
hasit_in = """\
if 'haveit' not in test_dict:
pass
"""
notit_in = """\
if 'xhaveit' not in test_dict:
pass
"""
hitt = timeit.timeit(stmt=hasit_try, setup=setup)
hii = timeit.timeit(stmt=hasit_in, setup=setup)
nit = timeit.timeit(stmt=notit_try, setup=setup)
nii = timeit.timeit(stmt=notit_in, setup=setup)
print("hiit: %6.2f"%hitt)
print("hii : %6.2f"%hii)
print("nit : %6.2f"%nit)
print("nii : %6.2f"%nii)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment