{ "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.019930540991481394\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": "2025-01-16T09:55:34.905918Z", "iopub.status.busy": "2025-01-16T09:55:34.905805Z", "iopub.status.idle": "2025-01-16T09:55:34.972575Z", "shell.execute_reply": "2025-01-16T09:55:34.972257Z" }, "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-01-16T09:55:34.974348Z", "iopub.status.busy": "2025-01-16T09:55:34.974241Z", "iopub.status.idle": "2025-01-16T09:55:34.975965Z", "shell.execute_reply": "2025-01-16T09:55:34.975723Z" }, "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-01-16T09:55:34.977275Z", "iopub.status.busy": "2025-01-16T09:55:34.977178Z", "iopub.status.idle": "2025-01-16T09:55:34.979348Z", "shell.execute_reply": "2025-01-16T09:55:34.978967Z" }, "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": "2025-01-16T09:55:34.980984Z", "iopub.status.busy": "2025-01-16T09:55:34.980821Z", "iopub.status.idle": "2025-01-16T09:55:34.982941Z", "shell.execute_reply": "2025-01-16T09:55:34.982640Z" }, "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-01-16T09:55:34.984421Z", "iopub.status.busy": "2025-01-16T09:55:34.984296Z", "iopub.status.idle": "2025-01-16T09:55:34.986144Z", "shell.execute_reply": "2025-01-16T09:55:34.985766Z" }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "from types import TracebackType" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "button": false, "execution": { "iopub.execute_input": "2025-01-16T09:55:34.987628Z", "iopub.status.busy": "2025-01-16T09:55:34.987503Z", "iopub.status.idle": "2025-01-16T09:55:34.990268Z", "shell.execute_reply": "2025-01-16T09:55:34.989999Z" }, "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-01-16T09:55:34.991555Z", "iopub.status.busy": "2025-01-16T09:55:34.991443Z", "iopub.status.idle": "2025-01-16T09:55:34.993148Z", "shell.execute_reply": "2025-01-16T09:55:34.992896Z" }, "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-01-16T09:55:34.994532Z", "iopub.status.busy": "2025-01-16T09:55:34.994428Z", "iopub.status.idle": "2025-01-16T09:55:35.017773Z", "shell.execute_reply": "2025-01-16T09:55:35.017214Z" }, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Stopping total time:\n", "0.020417874970007688\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-01-16T09:55:35.041204Z", "iopub.status.busy": "2025-01-16T09:55:35.040987Z", "iopub.status.idle": "2025-01-16T09:55:35.255897Z", "shell.execute_reply": "2025-01-16T09:55:35.255589Z" }, "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.020398624998051673\n", "0.04107737500453368\n", "0.06481354200514033\n", "0.08904879196779802\n", "0.11017662496306002\n", "0.13036508299410343\n", "0.15074933297000825\n", "0.17117312498157844\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "0.1922393330023624\n", "0.2125453749904409\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": "2025-01-16T09:55:35.257637Z", "iopub.status.busy": "2025-01-16T09:55:35.257509Z", "iopub.status.idle": "2025-01-16T09:55:35.281321Z", "shell.execute_reply": "2025-01-16T09:55:35.281006Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "0.019930540991481394" ] }, "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.12.8" }, "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 }