Skip to content

Instantly share code, notes, and snippets.

@matham
Created June 24, 2018 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matham/fb25048166e71cd52ffac360722e7e74 to your computer and use it in GitHub Desktop.
Save matham/fb25048166e71cd52ffac360722e7e74 to your computer and use it in GitHub Desktop.
from timeit import timeit
from random import randint
class Base(object):
pass
class Derived1(Base):
pass
class Derived2(Derived1):
def __init__(self):
super(Derived2, self).__init__()
self.d = {i + 10: i * 35 for i in range(100)}
def get_item_exists(self):
return self.d.get(randint(10, 109), None)
def get_item_not_exists(self):
return self.d.get(randint(200, 299), None)
def try_item_exists(self):
return self.d[randint(10, 109)]
def try_item_not_exists(self):
return self.d[randint(200, 299)]
class Basey(object):
pass
derived = Derived2()
def using_get(base_cls, meth):
if isinstance(derived, base_cls):
item = getattr(derived, meth)()
if item is not None:
return 1
else:
return 2
else:
return 2
def using_hasattr_with_try(meth):
f = getattr(derived, meth, None)
if f is None:
return 2
try:
return f()
except KeyError:
return 2
def using_hasattr_with_get(meth):
f = getattr(derived, meth, None)
if f is None:
return 2
if f() is not None:
return 1
else:
return 2
def using_try(meth):
try:
return getattr(derived, meth)()
except (AttributeError, KeyError):
return 2
number = 100000
print('Using get')
print(timeit(stmt='using_get(Base, "get_item_exists")', globals=globals(), number=number))
print(timeit(stmt='using_get(Base, "get_item_not_exists")', globals=globals(), number=number))
print(timeit(stmt='using_get(Basey, "get_item_exists")', globals=globals(), number=number))
print('Using try')
print(timeit(stmt='using_try("try_item_exists")', globals=globals(), number=number))
print(timeit(stmt='using_try("try_item_not_exists")', globals=globals(), number=number))
print(timeit(stmt='using_try("crapshoot")', globals=globals(), number=number))
print('Using hasattr with try')
print(timeit(stmt='using_hasattr_with_try("try_item_exists")', globals=globals(), number=number))
print(timeit(stmt='using_hasattr_with_try("try_item_not_exists")', globals=globals(), number=number))
print(timeit(stmt='using_hasattr_with_try("crapshoot")', globals=globals(), number=number))
print('Using hasattr with get')
print(timeit(stmt='using_hasattr_with_get("get_item_exists")', globals=globals(), number=number))
print(timeit(stmt='using_hasattr_with_get("get_item_not_exists")', globals=globals(), number=number))
print(timeit(stmt='using_hasattr_with_get("crapshoot")', globals=globals(), number=number))
Using get
0.19003750316542833
0.19243315464712535
0.02366930528082334
Using try
0.15584060542347994
0.20444854781406674
0.07282954331313385
Using hasattr with try
0.16538172991794986
0.19700993934624478
0.05458561431828435
Using hasattr with get
0.17889124477087037
0.1854579530374878
0.05219509863296956
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment