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 Program to Get the Full Path of the Current Working Directory
Python while Loop
Python Objects and Classes
Python Program to Make a Simple Calculator
Python Program to Display the multiplication Table
Python Exception Handling Using try, except and finally statement
Python Program to Randomly Select an Element From the List
Python complex()
Python *args and **kwargs
Python Statement, Indentation and Comments
Python String title()
Python for Loop
Python Tuple index()
Python Program to Find HCF or GCD
Python if...else Statement
Python Directory and Files Management
Python List index()
Python Program to Concatenate Two Lists
Python Set update()
Python String isdecimal()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Deep Learning with Python - Francois Cholletf
Python property()
Python Deep Learning Cookbook - Indra den Bakker
Python Program to Iterate Through Two Lists in Parallel
Python Program to Check Leap Year
Python next()
Python Set intersection_update()
Python Set intersection()
Python all()
Python Program to Find Numbers Divisible by Another Number
How to get current date and time in Python?