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 Safely Create a Nested Directory
Python open()
Python String partition()
Python len()
Python Program to Check If Two Strings are Anagram
Python callable()
Python Program to Find LCM
Python bool()
Python Program to Count the Occurrence of an Item in a List
Python String istitle()
Python Program to Differentiate Between type() and isinstance()
Python Tuple
Python Program to Find the Size (Resolution) of a Image
Python Program to Check Armstrong Number
Python Program to Print Output Without a Newline
Python Program to Access Index of a List Using for Loop
Python dict()
Python Program to Calculate the Area of a Triangle
Python Program to Iterate Over Dictionaries Using for Loop
Python Program to Sort Words in Alphabetic Order
Python next()
Python String find()
Python Set isdisjoint()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python set()
Python String rsplit()
Python List insert()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python List extend()
Python Program to Create a Countdown Timer
Python Program to Create a Long Multiline String
Python Program to Transpose a Matrix