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 len()
Python Program to Remove Punctuations From a String
Python Program to Differentiate Between del, remove, and pop on a List
Python Set symmetric_difference_update()
Python id()
Python all()
Python Global Keyword
Python String isspace()
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python Set pop()
Python Functions
Python Program to Sort a Dictionary by Value
Python sorted()
Python Set copy()
Python Set difference()
Python String rsplit()
Python String translate()
Python Program to Split a List Into Evenly Sized Chunks
Python RegEx
Python Program to Create Pyramid Patterns
Python String format()
Python Program to Find Hash of File
Python Program to Check If a List is Empty
Python Program to Transpose a Matrix
Python List
Python String swapcase()
Python issubclass()
Python String rjust()
Python ascii()
Python List remove()
Python frozenset()
Python Program to Swap Two Variables