{ "cells": [ { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "slide" } }, "source": [ "# Timer\n", "\n", "The code in this notebook helps with measuring time." ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "source": [ "**Prerequisites**\n", "\n", "* This notebook needs some understanding on advanced concepts in Python, notably \n", " * classes\n", " * the Python `with` statement\n", " * measuring time" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Synopsis\n", "\n", "\n", "To [use the code provided in this chapter](Importing.ipynb), write\n", "\n", "```python\n", ">>> from debuggingbook.Timer import \n", "```\n", "\n", "and then make use of the following features.\n", "\n", "\n", "The `Timer` class allows you to measure elapsed real time (in fractional seconds). Its typical usage is in conjunction with a `with` clause:\n", "\n", "```python\n", ">>> with Timer() as t:\n", ">>> some_long_running_function()\n", ">>> t.elapsed_time()\n", "0.02475320800112968\n", "```\n" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "slide" } }, "source": [ "## Measuring Time\n", "\n", "The class `Timer` allows measuring the elapsed time during some code execution." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "button": false, "execution": { "iopub.execute_input": "2023-11-12T13:02:39.996812Z", "iopub.status.busy": "2023-11-12T13:02:39.996667Z", "iopub.status.idle": "2023-11-12T13:02:40.028870Z", "shell.execute_reply": "2023-11-12T13:02:40.028570Z" }, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import bookutils.setup" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "button": false, "execution": { "iopub.execute_input": "2023-11-12T13:02:40.030857Z", "iopub.status.busy": "2023-11-12T13:02:40.030701Z", "iopub.status.idle": "2023-11-12T13:02:40.032449Z", "shell.execute_reply": "2023-11-12T13:02:40.032165Z" }, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import time" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2023-11-12T13:02:40.034080Z", "iopub.status.busy": "2023-11-12T13:02:40.033944Z", "iopub.status.idle": "2023-11-12T13:02:40.035465Z", "shell.execute_reply": "2023-11-12T13:02:40.035182Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "# ignore\n", "from typing import Type, Any" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "button": false, "execution": { "iopub.execute_input": "2023-11-12T13:02:40.037137Z", "iopub.status.busy": "2023-11-12T13:02:40.037022Z", "iopub.status.idle": "2023-11-12T13:02:40.038867Z", "shell.execute_reply": "2023-11-12T13:02:40.038620Z" }, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def clock() -> float:\n", " \"\"\"\n", " Return the number of fractional seconds elapsed since some point of reference.\n", " \"\"\"\n", " return time.perf_counter()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2023-11-12T13:02:40.040390Z", "iopub.status.busy": "2023-11-12T13:02:40.040283Z", "iopub.status.idle": "2023-11-12T13:02:40.041814Z", "shell.execute_reply": "2023-11-12T13:02:40.041568Z" }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "from types import TracebackType" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "button": false, "execution": { "iopub.execute_input": "2023-11-12T13:02:40.043241Z", "iopub.status.busy": "2023-11-12T13:02:40.043160Z", "iopub.status.idle": "2023-11-12T13:02:40.045888Z", "shell.execute_reply": "2023-11-12T13:02:40.045653Z" }, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "class Timer:\n", " def __init__(self) -> None:\n", " \"\"\"Constructor\"\"\"\n", " self.start_time = clock()\n", " self.end_time = None\n", "\n", " def __enter__(self) -> Any:\n", " \"\"\"Begin of `with` block\"\"\"\n", " self.start_time = clock()\n", " self.end_time = None\n", " return self\n", "\n", " def __exit__(self, exc_type: Type, exc_value: BaseException,\n", " tb: TracebackType) -> None:\n", " \"\"\"End of `with` block\"\"\"\n", " self.end_time = clock() # type: ignore\n", "\n", " def elapsed_time(self) -> float:\n", " \"\"\"Return elapsed time in seconds\"\"\"\n", " if self.end_time is None:\n", " # still running\n", " return clock() - self.start_time\n", " else:\n", " return self.end_time - self.start_time # type: ignore" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "source": [ "Here's an example:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "button": false, "execution": { "iopub.execute_input": "2023-11-12T13:02:40.047525Z", "iopub.status.busy": "2023-11-12T13:02:40.047401Z", "iopub.status.idle": "2023-11-12T13:02:40.049441Z", "shell.execute_reply": "2023-11-12T13:02:40.049118Z" }, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def some_long_running_function() -> None:\n", " i = 1000000\n", " while i > 0:\n", " i -= 1" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "button": false, "execution": { "iopub.execute_input": "2023-11-12T13:02:40.051054Z", "iopub.status.busy": "2023-11-12T13:02:40.050962Z", "iopub.status.idle": "2023-11-12T13:02:40.077674Z", "shell.execute_reply": "2023-11-12T13:02:40.077405Z" }, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Stopping total time:\n", "0.024604500000350527\n" ] } ], "source": [ "print(\"Stopping total time:\")\n", "with Timer() as t:\n", " some_long_running_function()\n", "print(t.elapsed_time())" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "button": false, "execution": { "iopub.execute_input": "2023-11-12T13:02:40.097692Z", "iopub.status.busy": "2023-11-12T13:02:40.097557Z", "iopub.status.idle": "2023-11-12T13:02:40.349326Z", "shell.execute_reply": "2023-11-12T13:02:40.349030Z" }, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Stopping time in between:\n", "0.024645499999678577\n", "0.04903750000084983\n", "0.0736815000000206\n", "0.09849070900054357\n", "0.12325487499947485\n", "0.14797141700000793\n", "0.17294362500069838\n", "0.1983805840009154\n", "0.22383649999937916\n", "0.24952462500004913\n" ] } ], "source": [ "print(\"Stopping time in between:\")\n", "with Timer() as t:\n", " for i in range(10):\n", " some_long_running_function()\n", " print(t.elapsed_time())" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "source": [ "That's it, folks – enjoy!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Synopsis\n", "\n", "The `Timer` class allows you to measure elapsed real time (in fractional seconds). Its typical usage is in conjunction with a `with` clause:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2023-11-12T13:02:40.351268Z", "iopub.status.busy": "2023-11-12T13:02:40.351048Z", "iopub.status.idle": "2023-11-12T13:02:40.379859Z", "shell.execute_reply": "2023-11-12T13:02:40.379471Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "0.02475320800112968" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with Timer() as t:\n", " some_long_running_function()\n", "t.elapsed_time()" ] }, { "cell_type": "markdown", "metadata": { "button": false, "new_sheet": true, "run_control": { "read_only": false }, "slideshow": { "slide_type": "slide" } }, "source": [ "## Lessons Learned\n", "\n", "* With the `Timer` class, it is very easy to measure elapsed time." ] } ], "metadata": { "ipub": { "bibliography": "fuzzingbook.bib", "toc": true }, "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.10.2" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true }, "toc-autonumbering": false }, "nbformat": 4, "nbformat_minor": 4 }