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
start
usingtime()
. - 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 eval()
Python map()
Deep Learning with Python - Francois Cholletf
Python Dictionary fromkeys()
Python sorted()
Python Program to Convert Celsius To Fahrenheit
Python String lstrip()
Python Program to Print the Fibonacci sequence
Python Program to Find LCM
Python Program to Find Sum of Natural Numbers Using Recursion
Python Program to Check Leap Year
Python Program to Compute the Power of a Number
Python setattr()
Python Program to Find HCF or GCD
Python List remove()
Python Program to Get the File Name From the File Path
Python String rindex()
Python timestamp to datetime and vice-versa
Python Variables, Constants and Literals
Python print()
Python repr()
Python Program to Find Hash of File
Python bool()
Python frozenset()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python super()
Python Dictionary items()
Python RegEx
Python zip()
Python Set issuperset()
Python Program to Solve Quadratic Equation
Python Program to Find the Size (Resolution) of a Image