{ "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 fuzzingbook.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.024238083002273925\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": "2024-01-18T17:31:15.201030Z", "iopub.status.busy": "2024-01-18T17:31:15.200460Z", "iopub.status.idle": "2024-01-18T17:31:15.261838Z", "shell.execute_reply": "2024-01-18T17:31:15.260810Z" }, "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": "2024-01-18T17:31:15.265829Z", "iopub.status.busy": "2024-01-18T17:31:15.265478Z", "iopub.status.idle": "2024-01-18T17:31:15.267685Z", "shell.execute_reply": "2024-01-18T17:31:15.267319Z" }, "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": "2024-01-18T17:31:15.269839Z", "iopub.status.busy": "2024-01-18T17:31:15.269654Z", "iopub.status.idle": "2024-01-18T17:31:15.271740Z", "shell.execute_reply": "2024-01-18T17:31:15.271297Z" }, "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": "2024-01-18T17:31:15.273449Z", "iopub.status.busy": "2024-01-18T17:31:15.273338Z", "iopub.status.idle": "2024-01-18T17:31:15.275307Z", "shell.execute_reply": "2024-01-18T17:31:15.275058Z" }, "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": "2024-01-18T17:31:15.277034Z", "iopub.status.busy": "2024-01-18T17:31:15.276925Z", "iopub.status.idle": "2024-01-18T17:31:15.278704Z", "shell.execute_reply": "2024-01-18T17:31:15.278446Z" }, "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "from types import TracebackType" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "button": false, "execution": { "iopub.execute_input": "2024-01-18T17:31:15.280269Z", "iopub.status.busy": "2024-01-18T17:31:15.280153Z", "iopub.status.idle": "2024-01-18T17:31:15.283050Z", "shell.execute_reply": "2024-01-18T17:31:15.282758Z" }, "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": "2024-01-18T17:31:15.284738Z", "iopub.status.busy": "2024-01-18T17:31:15.284593Z", "iopub.status.idle": "2024-01-18T17:31:15.286594Z", "shell.execute_reply": "2024-01-18T17:31:15.286284Z" }, "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": "2024-01-18T17:31:15.288349Z", "iopub.status.busy": "2024-01-18T17:31:15.288244Z", "iopub.status.idle": "2024-01-18T17:31:15.315444Z", "shell.execute_reply": "2024-01-18T17:31:15.315138Z" }, "new_sheet": false, "run_control": { "read_only": false }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Stopping total time:\n", "0.0249281249998603\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": "2024-01-18T17:31:15.336384Z", "iopub.status.busy": "2024-01-18T17:31:15.336240Z", "iopub.status.idle": "2024-01-18T17:31:15.589037Z", "shell.execute_reply": "2024-01-18T17:31:15.588730Z" }, "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.024789499992039055\n", "0.04986395899322815\n", "0.07536137499846518\n", "0.10000333399511874\n", "0.12546124999062158\n", "0.15078962499683257\n", "0.17644895899866242\n", "0.20118041698879097\n", "0.22568654199130833\n", "0.2503899999865098\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": "2024-01-18T17:31:15.590713Z", "iopub.status.busy": "2024-01-18T17:31:15.590605Z", "iopub.status.idle": "2024-01-18T17:31:15.618220Z", "shell.execute_reply": "2024-01-18T17:31:15.617928Z" }, "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "0.024238083002273925" ] }, "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 }