Created
October 25, 2019 21:05
-
-
Save nicktimko/8bf0611c36e0187e2f115583a767beeb to your computer and use it in GitHub Desktop.
Benchmark: excludes once vs. each time
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": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import attr\n", | |
"from attr.filters import exclude" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'a': 'a', 'b': 'b', 'c': 'c', 'd': 'd'}" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@attr.s(frozen=True, slots=True)\n", | |
"class TestThing:\n", | |
" a = attr.ib()\n", | |
" b = attr.ib()\n", | |
" c = attr.ib()\n", | |
" d = attr.ib()\n", | |
" w = attr.ib()\n", | |
" x = attr.ib()\n", | |
" y = attr.ib()\n", | |
" z = attr.ib()\n", | |
"\n", | |
" def as_dict(self):\n", | |
" d = attr.asdict(\n", | |
" self,\n", | |
" filter=exclude(\n", | |
" attr.fields(self.__class__).w,\n", | |
" attr.fields(self.__class__).x,\n", | |
" attr.fields(self.__class__).y,\n", | |
" attr.fields(self.__class__).z,\n", | |
" ),\n", | |
" )\n", | |
" return d\n", | |
" \n", | |
"TestThing(*'abcdwxyz').as_dict()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"22.1 µs ± 4.2 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"%%timeit tut = TestThing(1,2,3,4,5,6,7,8)\n", | |
"tut.as_dict()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'a': 'a', 'b': 'b', 'c': 'c', 'd': 'd'}" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"@attr.s(frozen=True, slots=True)\n", | |
"class TestThing2:\n", | |
" a = attr.ib()\n", | |
" b = attr.ib()\n", | |
" c = attr.ib()\n", | |
" d = attr.ib()\n", | |
" w = attr.ib()\n", | |
" x = attr.ib()\n", | |
" y = attr.ib()\n", | |
" z = attr.ib()\n", | |
" \n", | |
" def as_dict(self):\n", | |
" d = attr.asdict(\n", | |
" self,\n", | |
" filter=self.__class__.excludes,\n", | |
" )\n", | |
" return d\n", | |
"\n", | |
"TestThing2.excludes = exclude(\n", | |
" attr.fields(TestThing2).w,\n", | |
" attr.fields(TestThing2).x,\n", | |
" attr.fields(TestThing2).y,\n", | |
" attr.fields(TestThing2).z,\n", | |
")\n", | |
" \n", | |
"TestThing2(*'abcdwxyz').as_dict()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"14 µs ± 3.36 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"%%timeit tut2 = TestThing2(1,2,3,4,5,6,7,8)\n", | |
"tut2.as_dict()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.7.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment