Created
February 27, 2023 19:48
-
-
Save mahenzon/cf5aa8ffbcbeb10dec64f4c35b4b8120 to your computer and use it in GitHub Desktop.
Python timeit example w/ or w/o elif/else
This file contains 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1.6172812079894356 the Good\n", | |
"1.4139868330094032 the Bad\n", | |
"1.4188809999614023 the Ugly\n", | |
"1.5021539169829339 the Nice\n" | |
] | |
} | |
], | |
"source": [ | |
"from timeit import timeit\n", | |
"from functools import partial\n", | |
"\n", | |
"\n", | |
"def who_is_who(nickname):\n", | |
" if nickname == \"the Good\":\n", | |
" return \"Clint Eastwood\"\n", | |
" elif nickname == \"the Bad\":\n", | |
" return \"Lee Van Cleef\"\n", | |
" elif nickname == \"the Ugly\":\n", | |
" return \"Eli Wallach\"\n", | |
" else:\n", | |
" return \"Unknown\"\n", | |
"\n", | |
"for nick in [\n", | |
" \"the Good\",\n", | |
" \"the Bad\",\n", | |
" \"the Ugly\",\n", | |
" \"the Nice\",\n", | |
" ]:\n", | |
" func = partial(who_is_who, nickname=nick)\n", | |
" print(timeit(func, number=10_000_000), nick)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1.5060692499973811 the Good\n", | |
"1.2715134999598376 the Bad\n", | |
"1.4030666659818962 the Ugly\n", | |
"1.3945055829826742 the Nice\n" | |
] | |
} | |
], | |
"source": [ | |
"def who_is_who(nickname):\n", | |
" if nickname == \"the Good\":\n", | |
" return \"Clint Eastwood\"\n", | |
" if nickname == \"the Bad\":\n", | |
" return \"Lee Van Cleef\"\n", | |
" if nickname == \"the Ugly\":\n", | |
" return \"Eli Wallach\"\n", | |
" return \"Unknown\"\n", | |
"\n", | |
"\n", | |
"for nick in [\n", | |
" \"the Good\",\n", | |
" \"the Bad\",\n", | |
" \"the Ugly\",\n", | |
" \"the Nice\",\n", | |
" ]:\n", | |
" func = partial(who_is_who, nickname=nick)\n", | |
" print(timeit(func, number=10_000_000), nick)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"outputs": [], | |
"source": [], | |
"metadata": { | |
"collapsed": false | |
} | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.10.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment