{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "verbal-arrival", "metadata": {}, "outputs": [], "source": [ "# An example program to profile\n", "\n", "import numpy as np\n", "\n", "def test_me():\n", " for i in range(6):\n", " x = np.array(range(10**7))\n", " y = np.array(np.random.uniform(0, 100, size=(10**8)))\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "lesbian-premium", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The scalene extension is already loaded. To reload it, use:\n", " %reload_ext scalene\n" ] } ], "source": [ "# Load Scalene\n", "%load_ext scalene" ] }, { "cell_type": "code", "execution_count": 3, "id": "listed-keyboard", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
                                             [1]: % of time = 100.00% out of   6.02s.                                             \n",
       "        ╷        ╷       ╷       ╷                                                                                                 \n",
       "   Line Time    –––––– ––––––                                                                                                  \n",
       "        │Python  native system [1]                                                                                             \n",
       "╺━━━━━━━┿━━━━━━━━┿━━━━━━━┿━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸\n",
       "      1 │        │       │       │# An example program to profile                                                                  \n",
       "      2 │        │       │       │                                                                                                 \n",
       "      3 │        │       │       │import numpy as np                                                                               \n",
       "      4 │        │       │       │                                                                                                 \n",
       "      5 │        │       │       │def test_me():                                                                                   \n",
       "      6 │        │       │       │    for i in range(6):                                                                           \n",
       "      7     1%   35% │   7%  │        x = np.array(range(10**7))                                                               \n",
       "      8     2%   55% │       │        y = np.array(np.random.uniform(0, 100, size=(10**8)))                                    \n",
       "        │        │       │       │                                                                                                 \n",
       "╶───────┼────────┼───────┼───────┼────────────────────────────────────────────────────────────────────────────────────────────────╴\n",
       "        │        │       │       │function summary for <ipython-input-14-3a53cdc5d2d5>                                             \n",
       "      5     3%   91% │   6%  │test_me                                                                                          \n",
       "        ╵        ╵       ╵       ╵                                                                                                 \n",
       "
\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Profile just one line of code\n", "%scrun test_me()" ] }, { "cell_type": "code", "execution_count": 4, "id": "digital-ratio", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "usage: scalene [-h] [--version] [--outfile OUTFILE] [--html]\n", " [--reduced-profile] [--profile-interval PROFILE_INTERVAL]\n", " [--cpu-only] [--profile-all] [--profile-only PROFILE_ONLY]\n", " [--use-virtual-time]\n", " [--cpu-percent-threshold CPU_PERCENT_THRESHOLD]\n", " [--cpu-sampling-rate CPU_SAMPLING_RATE]\n", " [--malloc-threshold MALLOC_THRESHOLD]\n", "\n", "Scalene: a high-precision CPU and memory profiler, version 1.3.2\n", "https://github.com/plasma-umass/scalene\n", "\n", "command-line:\n", " % scalene [options] yourprogram.py\n", "or\n", " % python3 -m scalene [options] yourprogram.py\n", "\n", "in Jupyter, line mode:\n", " %scrun [options] statement\n", "\n", "in Jupyter, cell mode:\n", " %%scalene [options]\n", " code...\n", " code...\n", "\n", "optional arguments:\n", " -h, --help show this help message and exit\n", " --version prints the version number for this release of Scalene and exits\n", " --outfile OUTFILE file to hold profiler output (default: stdout)\n", " --html output as HTML (default: text)\n", " --reduced-profile generate a reduced profile, with non-zero lines only (default: False)\n", " --profile-interval PROFILE_INTERVAL\n", " output profiles every so many seconds (default: inf)\n", " --cpu-only only profile CPU time (default: profile CPU, memory, and copying)\n", " --profile-all profile all executed code, not just the target program (default: only the target program)\n", " --profile-only PROFILE_ONLY\n", " profile only code in files matching the given strings, separated by commas (default: no restrictions)\n", " --use-virtual-time measure only CPU time, not time spent in I/O or blocking (default: False)\n", " --cpu-percent-threshold CPU_PERCENT_THRESHOLD\n", " only report profiles with at least this percent of CPU time (default: 1%)\n", " --cpu-sampling-rate CPU_SAMPLING_RATE\n", " CPU sampling rate (default: every 0.01s)\n", " --malloc-threshold MALLOC_THRESHOLD\n", " only report profiles with at least this many allocations (default: 100)\n", "\n", "When running Scalene in the background, you can suspend/resume profiling\n", "for the process ID that Scalene reports. For example:\n", "\n", " % python3 -m scalene [options] yourprogram.py &\n", " Scalene now profiling process 12345\n", " to suspend profiling: python3 -m scalene.profile --off --pid 12345\n", " to resume profiling: python3 -m scalene.profile --on --pid 12345\n" ] } ], "source": [ "# A full list of options\n", "%scrun --help" ] }, { "cell_type": "code", "execution_count": 5, "id": "radio-feelings", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
                                             [1]: % of time = 100.00% out of   6.67s.                                             \n",
       "        ╷        ╷       ╷       ╷                                                                                                 \n",
       "   Line Time    –––––– ––––––                                                                                                  \n",
       "        │Python  native system [1]                                                                                             \n",
       "╺━━━━━━━┿━━━━━━━━┿━━━━━━━┿━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸\n",
       "    ... │        │       │       │                                                                                                 \n",
       "      7 │        │   34% │   7%  │        x = np.array(range(10**7))                                                               \n",
       "      8     2%   49% │   8%  │        y = np.array(np.random.uniform(0, 100, size=(10**8)))                                    \n",
       "        │        │       │       │                                                                                                 \n",
       "╶───────┼────────┼───────┼───────┼────────────────────────────────────────────────────────────────────────────────────────────────╴\n",
       "        │        │       │       │function summary for <ipython-input-14-3a53cdc5d2d5>                                             \n",
       "      5     2%   83% │  15%  │test_me                                                                                          \n",
       "        ╵        ╵       ╵       ╵                                                                                                 \n",
       "
\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Generate a reduced profile (only lines with non-zero counts)\n", "%scrun --reduced-profile test_me()" ] }, { "cell_type": "code", "execution_count": 6, "id": "minimal-society", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
         /var/folders/m7/sln1lr497jqcchb2ddh32rw00000gq/T/scalene_profile_qxy9xnh3.py: % of time = 100.00% out of   0.09s.         \n",
       "        ╷        ╷       ╷       ╷                                                                                                 \n",
       "   Line Time    –––––– ––––––                                                                                                  \n",
       "        │Python  native system /var/folders/m7/sln1lr497jqcchb2ddh32rw00000gq/T/scalene_profile_qxy9xnh3.py                     \n",
       "╺━━━━━━━┿━━━━━━━━┿━━━━━━━┿━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸\n",
       "    ... │        │       │       │                                                                                                 \n",
       "      4 │   21%  │       │       │    for j in range(1000):                                                                        \n",
       "      5    59%    6% │  13%  │        x += 1                                                                                   \n",
       "        ╵        ╵       ╵       ╵                                                                                                 \n",
       "
\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%scalene --reduced-profile\n", "# Profile more than one line of code in a cell\n", "x = 0\n", "for i in range(1000):\n", " for j in range(1000):\n", " x += 1" ] }, { "cell_type": "code", "execution_count": null, "id": "hungry-yesterday", "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.9.2" } }, "nbformat": 4, "nbformat_minor": 5 }