We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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
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>
The text was updated successfully, but these errors were encountered:
Other blogs that recommend outdated advice:
https://www.geeksforgeeks.org/python-map-vs-for-loop/ https://www.tutorialspoint.com/map-vs-for-loop-in-python
Sorry, something went wrong.
chrisvaillancourt
No branches or pull requests
compare findings from this blog to results in python 3.13.
Results on local machine:
Example python code
The text was updated successfully, but these errors were encountered: