Created
October 7, 2021 07:42
-
-
Save IgorZyktin/af2502ec184d0ee05c047af38314c8b3 to your computer and use it in GitHub Desktop.
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 random | |
import statistics | |
import string | |
import time | |
import uuid | |
VARIANTS = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048] | |
ks = [] | |
ds = [] | |
for k in VARIANTS: | |
TOTAL_RECORDS = 1000 * k | |
PATH_LENGTH = 100 | |
SAMPLE_SIZE = 100 * k | |
ATTEMPTS = 600 | |
RANDOM_PART = 0.1 | |
all_uuids = [str(uuid.uuid4()) | |
for _ in range(TOTAL_RECORDS)] | |
random_uuids = [str(uuid.uuid4()) | |
for _ in range(int(TOTAL_RECORDS * RANDOM_PART))] | |
storage = { | |
uuid: { | |
"uuid": uuid, | |
"number": number, | |
"path": ''.join( | |
random.choices(string.ascii_letters, k=PATH_LENGTH)) | |
} | |
for number, uuid in enumerate(all_uuids) | |
} | |
# approach 1 -------------------------- | |
results = [] | |
for _ in range(ATTEMPTS): | |
block = random.sample(all_uuids + random_uuids, SAMPLE_SIZE) | |
start = time.perf_counter() | |
output = [] | |
for uuid_ in block: | |
value = storage.get(uuid_) | |
if value is not None: | |
output.append(value) | |
duration = time.perf_counter() - start | |
results.append(duration) | |
print('Approach 1') | |
med1 = statistics.median(results) | |
print('Median:', med1) | |
print('Average:', statistics.mean(results)) | |
# approach 2 -------------------------- | |
results = [] | |
for _ in range(ATTEMPTS): | |
block = random.sample(all_uuids + random_uuids, SAMPLE_SIZE) | |
start = time.perf_counter() | |
output = [ | |
value | |
for uuid in block | |
if (value := storage.get(uuid)) | |
] | |
duration = time.perf_counter() - start | |
results.append(duration) | |
print() | |
print('Approach 2') | |
med2 = statistics.median(results) | |
print('Median:', med2) | |
print('Average:', statistics.mean(results)) | |
delta = (med1 - med2) / med2 | |
print() | |
print(f'Delta: {delta:%}') | |
ks.append(k) | |
ds.append(round(delta * 100, 1)) | |
print(ks) | |
print(ds) | |
# [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048] | |
# [72.5, 28.3, 29.3, 50.2, 31.4, 22.3, 14.5, 9.7, 7.8, 6.4, 5.9, 6.6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment