{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"# Trying the SnakeViz Jupyter extension for profiling\n",
"\n",
"I want to try the SnakeViz Jupyter extension for profiling Python code *right inside the Jupyter notebook*!\n",
"\n",
"- Author: [Lilian Besson](https://besson.link) ([@Naereen](https://GitHub.com/Naereen) ;\n",
"- License: [MIT License](https://lbesson.mit-license.org/) ;\n",
"- Date: 27-02-2021."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"## Installing and loading"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-27T13:04:49.589099Z",
"start_time": "2021-02-27T13:04:49.460824Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"python3 -m pip install snakeviz\n"
]
}
],
"source": [
"!python3 -m pip install snakeviz"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-27T13:05:05.557681Z",
"start_time": "2021-02-27T13:05:05.538526Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The snakeviz extension is already loaded. To reload it, use:\n",
" %reload_ext snakeviz\n"
]
}
],
"source": [
"%load_ext snakeviz"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
},
"tags": []
},
"source": [
"Let's try it!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-27T13:10:25.732244Z",
"start_time": "2021-02-27T13:10:25.726626Z"
}
},
"outputs": [],
"source": [
"def carre1(i):\n",
" return i**2\n",
"\n",
"N = 10000"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-27T13:10:29.455732Z",
"start_time": "2021-02-27T13:10:26.103730Z"
},
"slideshow": {
"slide_type": "subslide"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" \n",
"*** Profile stats marshalled to file '/tmp/tmpj_m5ith5'. \n",
"Embedding SnakeViz in this document...\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%snakeviz sum(carre1(i) for i in range(N))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"## A more serious example?\n",
"\n",
"Let's try this too!"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-27T13:11:45.754780Z",
"start_time": "2021-02-27T13:11:42.416577Z"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sum i**2 for i=0 to 10000 = 333383335000\n",
" \n",
"*** Profile stats marshalled to file '/tmp/tmpzkimd5wr'. \n",
"Embedding SnakeViz in this document...\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%snakeviz\n",
"\n",
"x = 0\n",
"for i in range(N+1):\n",
" x += i**2\n",
"\n",
"print(f\"Sum i**2 for i=0 to {N} = {x}\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"## My feeling about this extension\n",
"\n",
"I think it's quite useful, and well designed.\n",
"\n",
"It's completely \"over kill\" for any teaching at bac-2/bac+3 level (in France), but I keep it in mind, in case I need to use it for my own work."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"## Conclusion\n",
"\n",
"That was nice!\n",
"\n",
"Have a look at [my other notebooks](https://GitHub.com/Naereen/notebooks/)."
]
}
],
"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.6.9"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"toc-autonumbering": true,
"toc-showcode": true,
"toc-showmarkdowntxt": false,
"toc-showtags": false,
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}