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 any()
Python Strings
Python Program to Copy a File
Python Program to Illustrate Different Set Operations
Python datetime
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Program to Get Line Count of a File
Python Set update()
Python String isdigit()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python __import__()
Python Program to Find the Largest Among Three Numbers
Python Program to Create a Long Multiline String
Python List remove()
Python String lower()
Python Dictionary fromkeys()
Python frozenset()
Python Numbers, Type Conversion and Mathematics
Python Statement, Indentation and Comments
Python bin()
Python Program to Display Calendar
Python String maketrans()
Python String rindex()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Node.js vs Python for Backend Development
Python Function Arguments
Python Set add()
Python File I/O Operation
Python Program to Print all Prime Numbers in an Interval
Python repr()
Python Set issubset()
Python str()