Skip to content

Instantly share code, notes, and snippets.

@topher200
Last active May 6, 2016 01:25
Show Gist options
  • Save topher200/3771e62acde6145df658b4576cbdd33f to your computer and use it in GitHub Desktop.
Save topher200/3771e62acde6145df658b4576cbdd33f to your computer and use it in GitHub Desktop.
python decorator timing testing
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class TestClass(object):\n",
" def __init__(self, param1, param2, param3):\n",
" self.__param1 = param1\n",
" self.__param2 = param2\n",
" self.__param3 = param3\n",
" \n",
" def param1(self):\n",
" return self.__param1\n",
" \n",
" @property\n",
" def param2(self):\n",
" return self.__param2\n",
" \n",
" def param3():\n",
" def fget(self):\n",
" return self.__param3\n",
" return locals()\n",
" param3 = property(**param3())"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.36923384666\n",
"1.85793304443\n",
"1.87265896797\n"
]
}
],
"source": [
"test_class = TestClass(1, 2, 3)\n",
"\n",
"from timeit import timeit\n",
"print(timeit(\n",
" \"test_class.param1()\",\n",
" setup=\"from __main__ import TestClass; test_class = TestClass(1, 2, 3)\",\n",
" number=10000000\n",
" ))\n",
"\n",
"print(timeit(\n",
" \"test_class.param2\",\n",
" setup=\"from __main__ import TestClass; test_class = TestClass(1, 2, 3)\",\n",
" number=10000000\n",
" ))\n",
"\n",
"print(timeit(\n",
" \"test_class.param3\",\n",
" setup=\"from __main__ import TestClass; test_class = TestClass(1, 2, 3)\",\n",
" number=10000000\n",
" ))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment