Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add post on fact-checking python performance tips #254

Open
chrisvaillancourt opened this issue Jan 10, 2025 · 1 comment
Open

feat: add post on fact-checking python performance tips #254

chrisvaillancourt opened this issue Jan 10, 2025 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@chrisvaillancourt
Copy link
Owner

compare findings from this blog to results in python 3.13.

Results on local machine:

Execution time (s) with 1000000 iterations
------------------------------------
map_for_loop: 81.90313025000069s
map_for_loop_fn_call: 94.56856267900002s
map_built_in: 107.17973844499647s
map_built_in_lambda: 104.98461389599834s
map_list_comprehension: 68.50069368899858s
map_list_comprehension_fn_call: 90.32905048500106s
Example python code
import timeit
import random


NUMBER_OF_TEMPS = 1_000


random_temps = [random.randint(-1000, 1000) for _ in range(NUMBER_OF_TEMPS)]



def fahr_to_musk(f_temp):
    return (f_temp + 32) * (9 / 7)


def map_for_loop():
    mapped_temps = []

    for temp in random_temps:
        musk_temp = (temp + 32) * (9 / 7)
        mapped_temps.append(musk_temp)
    
    return mapped_temps


def map_for_loop_fn_call():
    mapped_temps = []

    for temp in random_temps:
        musk_temp = fahr_to_musk(temp)
        mapped_temps.append(musk_temp)
    
    return mapped_temps


def map_built_in():
    mapped_temps = list(map(fahr_to_musk, random_temps))
    return mapped_temps


def map_built_in_lambda():
    mapped_temps = list(map(lambda t: (t + 32) * (9 / 7), random_temps))
    return mapped_temps


def map_list_comprehension():
    mapped_temps = [((temp + 32) * (9 / 7)) for temp in random_temps]
    return mapped_temps


def map_list_comprehension_fn_call():
    mapped_temps = [fahr_to_musk(temp) for temp in random_temps]
    return mapped_temps

TEST_ITERATIONS = 1_000_000

def run_example():
    t_map_for_loop = timeit.timeit(map_for_loop, number=TEST_ITERATIONS)
    t_map_for_loop_fn_call = timeit.timeit(map_for_loop_fn_call, number=TEST_ITERATIONS)
    t_map_built_in = timeit.timeit(map_built_in, number=TEST_ITERATIONS)
    t_map_built_in_lambda = timeit.timeit(map_built_in_lambda, number=TEST_ITERATIONS)
    t_map_list_comprehension = timeit.timeit(map_list_comprehension, number=TEST_ITERATIONS)
    t_map_list_comprehension_fn_call = timeit.timeit(map_list_comprehension_fn_call, number=TEST_ITERATIONS)

    print('\n')
    print(f'Execution time (s) with {TEST_ITERATIONS} iterations')
    print('------------------------------------')
    print(f'map_for_loop: {t_map_for_loop}s')
    print(f'map_for_loop_fn_call: {t_map_for_loop_fn_call}s')
    print(f'map_built_in: {t_map_built_in}s')
    print(f'map_built_in_lambda: {t_map_built_in_lambda}s')
    print(f'map_list_comprehension: {t_map_list_comprehension}s')
    print(f'map_list_comprehension_fn_call: {t_map_list_comprehension_fn_call}s')





if __name__ == '__main__':
    run_example()```
</details>
@chrisvaillancourt chrisvaillancourt added the enhancement New feature or request label Jan 10, 2025
@chrisvaillancourt chrisvaillancourt self-assigned this Jan 10, 2025
@chrisvaillancourt
Copy link
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant