Created
January 15, 2021 08:02
-
-
Save Paulius-Maruska/8ca08c380467696aafff9a7b7e3d3340 to your computer and use it in GitHub Desktop.
Find what is length of a longest string in list (kind of), what is faster?
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 string | |
from functools import partial | |
from typing import Iterable, Tuple | |
from timeit import timeit | |
REPEAT = 1000 | |
CHARACTERS = string.digits + string.ascii_letters | |
CHARACTERS_FULL = string.printable | |
GENERATOR = random.SystemRandom() | |
def generate_random_string(length: int, characters: str = CHARACTERS, generator: random.Random = GENERATOR) -> str: | |
return "".join(generator.choices(CHARACTERS, k=length)) | |
def create_random_dataset(length: int, length_range: Tuple[int, int] = (1, 10), generator: random.Random = GENERATOR) -> Tuple[Tuple[str, str]]: | |
randint = generator.randint | |
value_gen = partial(generate_random_string, characters=CHARACTERS, generator=generator) | |
label_gen = partial(generate_random_string, characters=CHARACTERS_FULL, generator=generator) | |
result = tuple((value_gen(randint(*length_range)), label_gen(randint(10, 20))) for _ in range(length)) | |
return result | |
def get_longest_choice_1(choices: Iterable[Tuple[str, str]]) -> int: | |
result = 0 | |
for choice in choices: | |
value_length = len(choice[0]) | |
if result < value_length: | |
result = value_length | |
return result | |
def get_longest_choice_2(choices: Iterable[Tuple[str, str]]) -> int: | |
result = len(max(choices, key=lambda element: len(element[0]))[0]) | |
return result | |
def main(): | |
datasets = { | |
"random1_15_200": create_random_dataset(200, (1, 15)), | |
"random4_5_200": create_random_dataset(200, (4, 5)), | |
"random2_8_1000": create_random_dataset(1000, (2, 8)), | |
"random2_4_1000": create_random_dataset(1000, (2, 4)), | |
"random1_10_10000": create_random_dataset(10000, (1, 10)), | |
"random2_3_10000": create_random_dataset(10000, (2, 3)), | |
"samelength_500": create_random_dataset(500, (2, 2)), | |
} | |
functions = { | |
"get_longest_choice_1": get_longest_choice_1, | |
"get_longest_choice_2": get_longest_choice_2, | |
} | |
for key, val in datasets.items(): | |
print(f"Testing with dataset '{key}'") | |
globs = {**functions, "data": val} | |
result = timeit("get_longest_choice_1(data)", globals=globs, number=REPEAT) | |
print(f" get_longest_choice_1: {result}") | |
result = timeit("get_longest_choice_2(data)", globals=globs, number=REPEAT) | |
print(f" get_longest_choice_2: {result}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment