Last active
August 3, 2019 14:30
-
-
Save rbdixon/797d0478f9716c4123e401a1187552e1 to your computer and use it in GitHub Desktop.
Benchmark config lookup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Number of tasks: 1326
Speedup -30.40%
Baseline duration: 0.25ms
Alternative duration: 0.18ms