{ "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", "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", "**Note**: The examples in this section only work after the rest of the cells have been executed.\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2025-10-26T17:22:16.507417Z", "iopub.status.busy": "2025-10-26T17:22:16.507278Z", "iopub.status.idle": "2025-10-26T17:22:16.541041Z", "shell.execute_reply": "2025-10-26T17:22:16.540744Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "0.030439833004493266" ] }, "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": 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": "2025-10-26T17:22:15.441750Z", "iopub.status.busy": "2025-10-26T17:22:15.439816Z", "iopub.status.idle": "2025-10-26T17:22:16.126851Z", "shell.execute_reply": "2025-10-26T17:22:16.126574Z" }, "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": "2025-10-26T17:22:16.128639Z", "iopub.status.busy": "2025-10-26T17:22:16.128545Z", "iopub.status.idle": "2025-10-26T17:22:16.130176Z", "shell.execute_reply": "2025-10-26T17:22:16.129956Z" }, "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": "2025-10-26T17:22:16.131607Z", "iopub.status.busy": "2025-10-26T17:22:16.131513Z", "iopub.status.idle": "2025-10-26T17:22:16.132960Z", "shell.execute_reply": "2025-10-26T17:22:16.132736Z" }, "slideshow": { "slide_type": "fragment" }, "tags": [ "remove-input" ] }, "outputs": [], "source": [ "# ignore\n", "from typing import Type, Any" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "button": false, "execution": { "iopub.execute_input": "2025-10-26T17:22:16.134174Z", "iopub.status.busy": "2025-10-26T17:22:16.134088Z", "iopub.status.idle": "2025-10-26T17:22:16.135678Z", "shell.execute_reply": "2025-10-26T17:22:16.135421Z" }, "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": "2025-10-26T17:22:16.136852Z", "iopub.status.busy": "2025-10-26T17:22:16.136777Z", "iopub.status.idle": "2025-10-26T17:22:16.138137Z", "shell.execute_reply": "2025-10-26T17:22:16.137905Z" }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "from types import TracebackType" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "button": false, "execution": { "iopub.execute_input": "2025-10-26T17:22:16.139381Z", "iopub.status.busy": "2025-10-26T17:22:16.139293Z", "iopub.status.idle": "2025-10-26T17:22:16.141545Z", "shell.execute_reply": "2025-10-26T17:22:16.141300Z" }, "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": "2025-10-26T17:22:16.142989Z", "iopub.status.busy": "2025-10-26T17:22:16.142890Z", "iopub.status.idle": "2025-10-26T17:22:16.144440Z", "shell.execute_reply": "2025-10-26T17:22:16.144233Z" }, "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": "2025-10-26T17:22:16.145630Z", "iopub.status.busy": "2025-10-26T17:22:16.145537Z", "iopub.status.idle": "2025-10-26T17:22:16.178523Z", "shell.execute_reply": "2025-10-26T17:22:16.178251Z" }, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Stopping total time:\n", "0.03074662500876002\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": "2025-10-26T17:22:16.197717Z", "iopub.status.busy": "2025-10-26T17:22:16.197604Z", "iopub.status.idle": "2025-10-26T17:22:16.505711Z", "shell.execute_reply": "2025-10-26T17:22:16.505388Z" }, "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.030248791998019442\n", "0.060225916997296736\n", "0.09011208399897441\n", "0.12019625000539236\n", "0.1499536250194069\n", "0.17993966699577868\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "0.21137279199319892\n", "0.24325558400596492\n", "0.2745761669939384\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "0.30587045900756493\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": { "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.13.4" }, "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 }