Last active
November 10, 2018 23:05
-
-
Save tynor/e72ed72791e3f6469e5e3ae78484ff17 to your computer and use it in GitHub Desktop.
Lookup in list vs dict
This file contains 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
..................... | |
dict[int]: Mean +- std dev: 28.2 ns +- 1.3 ns | |
..................... | |
list[int]: Mean +- std dev: 105 ns +- 5 ns |
This file contains 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 perf | |
def bench_dict(loops, d): | |
range_it = range(loops) | |
t0 = perf.perf_counter() | |
for _ in range_it: | |
8 in d | |
3 in d | |
8 in d | |
4 in d | |
2 in d | |
3 in d | |
2 in d | |
7 in d | |
9 in d | |
7 in d | |
return perf.perf_counter() - t0 | |
def bench_list(loops, l): | |
range_it = range(loops) | |
t0 = perf.perf_counter() | |
for _ in range_it: | |
8 in l | |
3 in l | |
8 in l | |
4 in l | |
2 in l | |
3 in l | |
2 in l | |
7 in l | |
9 in l | |
7 in l | |
return perf.perf_counter() - t0 | |
d = {k: k for k in range(60)} | |
l = list(range(60)) | |
runner = perf.Runner() | |
runner.bench_time_func('dict[int]', bench_dict, d, inner_loops=10) | |
runner.bench_time_func('list[int]', bench_list, l, inner_loops=10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment