Table of Contents
In this example, you will learn to measure the elapsed time.
To understand this example, you should have the knowledge of the following Python programming topics:
1. Example 1: Using time module
import time start = time.time() print(23*2.3) end = time.time() print(end - start)
Output
52.9 3.600120544433594e-05
In order to calculate the time elapsed in executing a code, the time module can be used.
- Save the timestamp at the beginning of the code
startusingtime(). - Save the timestamp at the end of the code
end. - Find the difference between the end and start, which gives the execution time.
The execution time depends on the system.
2. Example 2: Using timeit module
from timeit import default_timer as timer start = timer() print(23*2.3) end = timer() print(end - start)
Output
52.9 6.355400000000039e-05
Similar to Example 1, we use timer() method from timeit module.
timeit provides the most accurate results.
Related posts:
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Program to Sort a Dictionary by Value
Python for Loop
Python Program to Get the File Name From the File Path
Python Program to Count the Number of Digits Present In a Number
Python dict()
Python Data Types
Python Program to Check If Two Strings are Anagram
Python Set pop()
Python List index()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python String isalnum()
Python Program to Find Sum of Natural Numbers Using Recursion
Python String strip()
Python Namespace and Scope
Python hex()
Python Program to Sort Words in Alphabetic Order
Python Function Arguments
Python Program to Extract Extension From the File Name
Python int()
Python Program to Solve Quadratic Equation
Python Program to Count the Number of Occurrence of a Character in String
Python Program to Generate a Random Number
Python Program to Capitalize the First Character of a String
Python open()
Python Program to Multiply Two Matrices
Python len()
Python Program to Check If a String Is a Number (Float)
Python globals()
Python Object Oriented Programming
Python Program to Find Hash of File
Python String isdigit()