Created
October 7, 2021 05:17
-
-
Save pepoluan/d5778d826cc57fbf825fc797204e8a52 to your computer and use it in GitHub Desktop.
Sort By .items() vs Sort By .keys()
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 pprint | |
import random | |
import timeit | |
MEMBS_COUNT = 100 | |
indexes = list(range(MEMBS_COUNT)) | |
random.shuffle(indexes) | |
orig_dict = {} | |
work_dict = {} | |
def sort_items(): | |
sm = 0 | |
for sz, flist in sorted(work_dict.items()): | |
sm += len(flist) | |
return sm | |
def sort_keys(): | |
sm = 0 | |
for sz in sorted(work_dict.keys()): | |
flist = work_dict[sz] | |
sm += len(flist) | |
return sm | |
results = {} | |
for iteration in range(5): | |
print(f"Iteration #{iteration}") | |
orig_dict = {i: [random.randint(0, 100) for _ in range(random.randint(2, 20))] for i in indexes} | |
for fun in ["sort_items", "sort_keys"]: | |
print(f"{fun}:\n ", end="") | |
# noinspection PyRedeclaration | |
work_dict = orig_dict.copy() | |
one_result = timeit.repeat(f"{fun}()", globals=globals(), number=100000) | |
print(one_result) | |
results.setdefault(fun, []).extend(one_result) | |
pprint.pprint(results) |
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
Iteration #0 | |
sort_items: | |
[2.2665747, 2.2948671, 2.2741954, 2.3581256, 2.246713099999999] | |
sort_keys: | |
[1.6133939000000002, 1.5604682000000007, 1.5670964999999981, 1.5475289999999973, 1.5408910999999996] | |
Iteration #1 | |
sort_items: | |
[2.3382344999999987, 2.3232704999999996, 2.2793155000000027, 2.3203272000000013, 2.2972348999999994] | |
sort_keys: | |
[1.6016799000000006, 1.4441036999999994, 1.5775312000000028, 1.604745600000001, 1.5907763000000017] | |
Iteration #2 | |
sort_items: | |
[2.322138899999999, 2.3223701000000005, 2.339320200000003, 2.2799884000000006, 2.3741212000000047] | |
sort_keys: | |
[1.6397268999999994, 1.6084398000000064, 1.6292432999999988, 1.6413487000000018, 1.6348839999999996] | |
Iteration #3 | |
sort_items: | |
[2.3771400999999983, 2.349693700000003, 2.276641099999999, 2.284355000000005, 2.3402518000000043] | |
sort_keys: | |
[1.5678709000000026, 1.6302175999999946, 1.5701210000000003, 1.6141111000000024, 1.6115692999999993] | |
Iteration #4 | |
sort_items: | |
[2.329644899999991, 2.29828950000001, 2.383834500000006, 2.3755140000000097, 2.368676999999991] | |
sort_keys: | |
[1.6295487000000008, 1.6385052999999914, 1.6402142000000026, 1.6683847999999983, 1.713991699999994] | |
{'sort_items': [2.2665747, | |
2.2948671, | |
2.2741954, | |
2.3581256, | |
2.246713099999999, | |
2.3382344999999987, | |
2.3232704999999996, | |
2.2793155000000027, | |
2.3203272000000013, | |
2.2972348999999994, | |
2.322138899999999, | |
2.3223701000000005, | |
2.339320200000003, | |
2.2799884000000006, | |
2.3741212000000047, | |
2.3771400999999983, | |
2.349693700000003, | |
2.276641099999999, | |
2.284355000000005, | |
2.3402518000000043, | |
2.329644899999991, | |
2.29828950000001, | |
2.383834500000006, | |
2.3755140000000097, | |
2.368676999999991], | |
'sort_keys': [1.6133939000000002, | |
1.5604682000000007, | |
1.5670964999999981, | |
1.5475289999999973, | |
1.5408910999999996, | |
1.6016799000000006, | |
1.4441036999999994, | |
1.5775312000000028, | |
1.604745600000001, | |
1.5907763000000017, | |
1.6397268999999994, | |
1.6084398000000064, | |
1.6292432999999988, | |
1.6413487000000018, | |
1.6348839999999996, | |
1.5678709000000026, | |
1.6302175999999946, | |
1.5701210000000003, | |
1.6141111000000024, | |
1.6115692999999993, | |
1.6295487000000008, | |
1.6385052999999914, | |
1.6402142000000026, | |
1.6683847999999983, | |
1.713991699999994]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment