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
startusingtime(). - 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 while Loop
Python min()
Python Program to Access Index of a List Using for Loop
Deep Learning with Python - Francois Cholletf
Python Program to Return Multiple Values From a Function
Python String expandtabs()
Python File I/O Operation
Python Program to Iterate Over Dictionaries Using for Loop
Python Program to Convert Decimal to Binary Using Recursion
Python Program to Remove Duplicate Element From a List
Python del Statement
Python Matrices and NumPy Arrays
Python Set isdisjoint()
Python Program to Multiply Two Matrices
Python Recursion
Python Operator Overloading
Python Dictionary fromkeys()
Python String join()
Python memoryview()
Python hex()
Python Type Conversion and Type Casting
Python Program to Merge Two Dictionaries
Python Set clear()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python List copy()
Python Errors and Built-in Exceptions
Python Data Structures and Algorithms - Benjamin Baka
Python Program to Check if a Number is Odd or Even
Python any()
Python Set union()
Python List append()
Python Program to Check Prime Number