{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "<small><small><i>\n", "All the IPython Notebooks in this example series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/90_Python_Examples/tree/main/07_Python_Advanced_examples)**\n", "</i></small></small>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Program to Measure the Elapsed Time in Python\n", "\n", "In this example, you will learn to measure the elapsed time.\n", "\n", "To understand this example, you should have the knowledge of the following **[Python programming](https://github.com/milaan9/01_Python_Introduction/blob/main/000_Intro_to_Python.ipynb)** topics:\n", "\n", "* **[Python time Module](https://github.com/milaan9/08_Python_Date_Time_Module/blob/main/007_Python_time_Module.ipynb)**" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2021-07-02T06:58:59.045097Z", "start_time": "2021-07-02T06:58:59.034355Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "52.9\n", "0.0\n" ] }, { "data": { "text/plain": [ "'\\n>>Expected output:\\n \\n52.9\\n0.0\\n'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Example 1: Using time module\n", "\n", "import time\n", "\n", "start = time.time()\n", "\n", "print(23*2.3)\n", "\n", "end = time.time()\n", "print(end - start)\n", "\n", "'''\n", ">>Expected output:\n", " \n", "52.9\n", "0.0\n", "'''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The execution time depends on the system." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2021-07-02T06:59:36.437432Z", "start_time": "2021-07-02T06:59:36.418880Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "52.9\n", "0.00035429999996949846\n" ] } ], "source": [ "# Example 2: Using timeit module\n", "\n", "from timeit import default_timer as timer\n", "\n", "start = timer()\n", "\n", "print(23*2.3)\n", "\n", "end = timer()\n", "print(end - start)\n", "\n", "\n", "'''\n", ">>Expected output:\n", " \n", "52.9\n", "0.00035429999996949846\n", "'''" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**`timeit`** provides the most accurate results. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "hide_input": false, "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.8.8" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }