Skip to content

Instantly share code, notes, and snippets.

@DylanLukes
Created October 21, 2023 21:10
Show Gist options
  • Save DylanLukes/d4ef02081f321be1b5cca51209b5bae3 to your computer and use it in GitHub Desktop.
Save DylanLukes/d4ef02081f321be1b5cca51209b5bae3 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 30,
"id": "63b0a187-8866-4376-b688-1cb5601314ff",
"metadata": {},
"outputs": [],
"source": [
"import dis\n",
"import faker"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "8064dbca-a7af-4160-8f6d-dfd77f550f9b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Python 3.12.0\n"
]
}
],
"source": [
"!python --version"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "83dc9403-ee51-4102-b608-bea7d12722a7",
"metadata": {},
"outputs": [],
"source": [
"fake = Faker()\n",
"fake.seed(0)"
]
},
{
"cell_type": "markdown",
"id": "57074fdc-674f-4eb8-b34c-488cac24d951",
"metadata": {},
"source": [
"## Sample Data"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "780b457d-a87d-4ca6-9330-314f3ad0440b",
"metadata": {},
"outputs": [],
"source": [
"N = 10**2\n",
"small_dict_1 = {fake.uuid4(): fake.uuid4() for _ in range(N)}\n",
"small_dict_2 = {fake.uuid4(): fake.uuid4() for _ in range(N)}"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "0bf83fee-334f-463b-90ce-93f96025891b",
"metadata": {},
"outputs": [],
"source": [
"N = 10**6\n",
"big_dict_1 = {fake.uuid4(): fake.uuid4() for _ in range(N)}\n",
"big_dict_2 = {fake.uuid4(): fake.uuid4() for _ in range(N)}"
]
},
{
"cell_type": "markdown",
"id": "557457a5-9335-488b-95fa-3c1ecb067cc8",
"metadata": {},
"source": [
"## Definitions"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "92d89e58-7266-4789-ab68-0869c1bde3c5",
"metadata": {},
"outputs": [],
"source": [
"def merge_splat(d1, d2):\n",
" return {**d1, **d2}\n",
"\n",
"def merge_union(d1, d2):\n",
" return d1 | d2\n",
"\n",
"def merge_update(d1, d2):\n",
" return d1.update(d2)"
]
},
{
"cell_type": "markdown",
"id": "21fdb311-8e25-471e-acc8-e41cc1fd6820",
"metadata": {},
"source": [
"## Dissasembly"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "38fc1c81-3196-4d95-bdca-aa2dfb37efc8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 1 0 RESUME 0\n",
"\n",
" 2 2 BUILD_MAP 0\n",
" 4 LOAD_FAST 0 (d1)\n",
" 6 DICT_UPDATE 1\n",
" 8 LOAD_FAST 1 (d2)\n",
" 10 DICT_UPDATE 1\n",
" 12 RETURN_VALUE\n"
]
}
],
"source": [
"dis.dis(merge_splat)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "fb9d309d-bede-4d8f-a808-ce13113640c7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 4 0 RESUME 0\n",
"\n",
" 5 2 LOAD_FAST 0 (d1)\n",
" 4 LOAD_FAST 1 (d2)\n",
" 6 BINARY_OP 7 (|)\n",
" 10 RETURN_VALUE\n"
]
}
],
"source": [
"dis.dis(merge_union)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "7141a3c5-fe6e-49ce-961c-5755186c660c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 7 0 RESUME 0\n",
"\n",
" 8 2 LOAD_FAST 0 (d1)\n",
" 4 LOAD_ATTR 1 (NULL|self + update)\n",
" 24 LOAD_FAST 1 (d2)\n",
" 26 CALL 1\n",
" 34 RETURN_VALUE\n"
]
}
],
"source": [
"dis.dis(merge_update)"
]
},
{
"cell_type": "markdown",
"id": "43efe8af-bf80-4c30-a0a5-e516ca1da23f",
"metadata": {},
"source": [
"## Performance"
]
},
{
"cell_type": "markdown",
"id": "67e78c2a-ad5f-4efb-af3a-7c1d07a493cd",
"metadata": {},
"source": [
"### Large Dicts"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "d7f3b191-013d-4d83-9c66-517c63541e2f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"121 ms ± 2.22 ms per loop (mean ± std. dev. of 10 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit -n 10 -r 10 merge_splat(big_dict_1, big_dict_2)"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "fddb6b32-757d-4b75-b21a-7432b11b0cfd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"120 ms ± 1.02 ms per loop (mean ± std. dev. of 10 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit -n 10 -r 10 merge_union(big_dict_1, big_dict_2)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "8bdb743e-2837-4d7b-b4cb-e07eec94f11d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"101 ms ± 1.34 ms per loop (mean ± std. dev. of 10 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit -n 10 -r 10 merge_update(big_dict_1, big_dict_2)"
]
},
{
"cell_type": "markdown",
"id": "fec5b88a-1a75-439b-b893-17925082e3ba",
"metadata": {},
"source": [
"### Small Dicts"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "01dcceba-1799-4c3a-b0fb-b23ea648c587",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8.53 µs ± 1.2 µs per loop (mean ± std. dev. of 10 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit -n 10 -r 10 merge_splat(small_dict_1, small_dict_2)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "356442ec-7f29-4285-ace1-c0065ee85efb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9.75 µs ± 1.58 µs per loop (mean ± std. dev. of 10 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit -n 10 -r 10 merge_union(small_dict_1, small_dict_2)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "ae565eb1-42a3-463f-96f4-9c135a714fc4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5 µs ± 854 ns per loop (mean ± std. dev. of 10 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit -n 10 -r 10 merge_update(small_dict_1, small_dict_2)"
]
}
],
"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.12.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment