Skip to content

Instantly share code, notes, and snippets.

@nopdotcom
Created June 1, 2021 16:01
Show Gist options
  • Save nopdotcom/ad99eb93d1d5ded4ac141a95736313db to your computer and use it in GitHub Desktop.
Save nopdotcom/ad99eb93d1d5ded4ac141a95736313db to your computer and use it in GitHub Desktop.
Python 3.9 on MBA M1: hash of functions, vs elif
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "abandoned-recognition",
"metadata": {},
"source": [
"## Is it better to write \"elif\" or use a hash of functions?\n",
"\n",
"\n",
"On a MacBook Air M1, the crossover point is 7. If you have 7 or more elif arms, consider switching to a hash of functions.\n",
"\n",
"|N|hash|elif|\n",
"|----|----|----|\n",
"|5|89.3|77.8|\n",
"|7|88|88.8|\n",
"|10|87.3|107|\n",
"\n",
"The numbers below are a fresh run."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "magnetic-system",
"metadata": {},
"outputs": [],
"source": [
"N = 7\n",
"fs = {}\n",
"res = 0"
]
},
{
"cell_type": "markdown",
"id": "novel-extraction",
"metadata": {},
"source": [
"Functions `function0`..`functionN` are created, which all set a global variable. They are injected into the `fs[]` hash."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "private-harvest",
"metadata": {},
"outputs": [],
"source": [
"for i in range(N):\n",
" exec(f\"def function{i}(): res = '{i}'\")\n",
" exec(f\"fs[{i}] = function{i}\")"
]
},
{
"cell_type": "markdown",
"id": "quality-hearts",
"metadata": {},
"source": [
"Produce some cut&pasteable code for timeit."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "medical-birth",
"metadata": {},
"outputs": [],
"source": [
"def big_elseif(n, indent):\n",
" s = indent\n",
" for i in range(n):\n",
" s = s + f\"\"\"if x == {i}: res= '{i}'\\n{indent}el\"\"\"\n",
" s = s + \"se: raise\"\n",
" return s"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "north-gospel",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"if x == 0: res= '0'\n",
"elif x == 1: res= '1'\n",
"elif x == 2: res= '2'\n",
"elif x == 3: res= '3'\n",
"elif x == 4: res= '4'\n",
"elif x == 5: res= '5'\n",
"elif x == 6: res= '6'\n",
"else: raise\n"
]
}
],
"source": [
"print(big_elseif(N, \"\"))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "governmental-reputation",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"91.8 ns ± 0.748 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)\n"
]
}
],
"source": [
"%%timeit x=0\n",
"x = (x + 1) % N\n",
"f = fs[x]\n",
"f()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "living-holiday",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"90.7 ns ± 0.603 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)\n"
]
}
],
"source": [
"%%timeit x=0\n",
"x = (x + 1) % N\n",
"if x == 0: res= '0'\n",
"elif x == 1: res= '1'\n",
"elif x == 2: res= '2'\n",
"elif x == 3: res= '3'\n",
"elif x == 4: res= '4'\n",
"elif x == 5: res= '5'\n",
"elif x == 6: res= '6'\n",
"else: raise"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment