{ "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 fuzzingbook.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-26T13:40:18.424167Z", "iopub.status.busy": "2025-10-26T13:40:18.424054Z", "iopub.status.idle": "2025-10-26T13:40:18.458417Z", "shell.execute_reply": "2025-10-26T13:40:18.458123Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "0.030869709007674828" ] }, "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-26T13:40:17.317019Z", "iopub.status.busy": "2025-10-26T13:40:17.316664Z", "iopub.status.idle": "2025-10-26T13:40:18.040375Z", "shell.execute_reply": "2025-10-26T13:40:18.040092Z" }, "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-26T13:40:18.041860Z", "iopub.status.busy": "2025-10-26T13:40:18.041776Z", "iopub.status.idle": "2025-10-26T13:40:18.043370Z", "shell.execute_reply": "2025-10-26T13:40:18.043136Z" }, "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-26T13:40:18.044502Z", "iopub.status.busy": "2025-10-26T13:40:18.044416Z", "iopub.status.idle": "2025-10-26T13:40:18.045968Z", "shell.execute_reply": "2025-10-26T13:40:18.045708Z" }, "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-26T13:40:18.047180Z", "iopub.status.busy": "2025-10-26T13:40:18.047091Z", "iopub.status.idle": "2025-10-26T13:40:18.048626Z", "shell.execute_reply": "2025-10-26T13:40:18.048410Z" }, "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-26T13:40:18.049720Z", "iopub.status.busy": "2025-10-26T13:40:18.049626Z", "iopub.status.idle": "2025-10-26T13:40:18.051016Z", "shell.execute_reply": "2025-10-26T13:40:18.050818Z" }, "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-26T13:40:18.052089Z", "iopub.status.busy": "2025-10-26T13:40:18.052016Z", "iopub.status.idle": "2025-10-26T13:40:18.054318Z", "shell.execute_reply": "2025-10-26T13:40:18.054127Z" }, "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-26T13:40:18.055467Z", "iopub.status.busy": "2025-10-26T13:40:18.055394Z", "iopub.status.idle": "2025-10-26T13:40:18.056841Z", "shell.execute_reply": "2025-10-26T13:40:18.056639Z" }, "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-26T13:40:18.057879Z", "iopub.status.busy": "2025-10-26T13:40:18.057800Z", "iopub.status.idle": "2025-10-26T13:40:18.089998Z", "shell.execute_reply": "2025-10-26T13:40:18.089692Z" }, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Stopping total time:\n", "0.03015562499058433\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-26T13:40:18.110104Z", "iopub.status.busy": "2025-10-26T13:40:18.109949Z", "iopub.status.idle": "2025-10-26T13:40:18.422679Z", "shell.execute_reply": "2025-10-26T13:40:18.422364Z" }, "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.0305006249982398\n", "0.0604882909974549\n", "0.09118766599567607\n", "0.12214049999602139\n", "0.15291795798111707\n", "0.18302566598867998\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "0.21359354100422934\n", "0.2463232500012964\n", "0.2783738329890184\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "0.3103841659903992\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 }