Skip to content

Instantly share code, notes, and snippets.

@akx
Created June 16, 2016 08:00
Show Gist options
  • Select an option

  • Save akx/7eec0e49651364a58278919331592579 to your computer and use it in GitHub Desktop.

Select an option

Save akx/7eec0e49651364a58278919331592579 to your computer and use it in GitHub Desktop.
import timeit
from collections import Counter
from enum import Enum
class E1(Enum):
A = 1
B = 2
class E2(Enum):
A = 1
B = 2
def get_prep_value_orig(value):
return (None if value is None else value.value)
def get_prep_value_patch(value):
return None if value is None else E1(value).value
def get_prep_value_typecheck(value):
if value is None:
return None
if not isinstance(value, E1):
return E1(value).value
return value.value
results = {}
t_results = Counter()
for fn in (get_prep_value_orig, get_prep_value_patch, get_prep_value_typecheck):
for val in (E1.A, 1, None):
test_name = '%s(%s)' % (fn.__name__, val)
try:
def w():
assert fn(val) == (1 if val else None)
result = timeit.timeit(w)
results[test_name] = result
t_results[fn.__name__] += result
except Exception as e:
print('%s: dnf (%s)' % (test_name, e))
t_results[fn.__name__] += 999
for name, time in sorted(results.items(), key=lambda kv: kv[1]):
print(name, time)
for name, result in t_results.most_common()[::-1]:
print(name, result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment