Skip to content

Instantly share code, notes, and snippets.

@rbdixon
Last active August 3, 2019 14:30
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 rbdixon/797d0478f9716c4123e401a1187552e1 to your computer and use it in GitHub Desktop.
Save rbdixon/797d0478f9716c4123e401a1187552e1 to your computer and use it in GitHub Desktop.
Benchmark config lookup
import timeit
import string
from itertools import combinations
ITERATIONS = 10000
TASK = 'task:'
d = {TASK + letter: True for letter in 'A'}
task_names = [''.join(x) for x in combinations(string.ascii_letters, 2)]
print(f'Number of tasks: {len(task_names)}')
current='''
for task_name in task_names:
key = TASK + task_name
d.get(key, False)
'''
alternative='''
for task_name in task_names:
key = TASK + task_name
if key in d:
d[key]
'''
data = locals()
current_time = timeit.timeit(current, globals=data, number=ITERATIONS)
alternative_time = timeit.timeit(alternative, globals=data, number=ITERATIONS)
speedup = (alternative_time - current_time) / current_time * 100
print(f'Speedup {speedup:.2f}%')
print(f'Baseline duration: {current_time/ITERATIONS*1e3:.2}ms')
print(f'Alternative duration: {alternative_time/ITERATIONS*1e3:.2}ms')
@rbdixon
Copy link
Author

rbdixon commented Aug 3, 2019

Number of tasks: 1326
Speedup -30.40%
Baseline duration: 0.25ms
Alternative duration: 0.18ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment