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:
Introduction to Scientific Programming with Python - Joakim Sundnes
Python String upper()
Python Closures
Python Program to Calculate the Area of a Triangle
Python object()
Python Program to Find ASCII Value of Character
Python super()
Python Program to Find the Largest Among Three Numbers
Python pass statement
Python Program to Find Sum of Natural Numbers Using Recursion
Python String rsplit()
Python if...else Statement
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python File I/O Operation
Python hasattr()
Python Program to Sort a Dictionary by Value
Python List copy()
Python Program to Get the Full Path of the Current Working Directory
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python globals()
Python Objects and Classes
Python Program to Display Calendar
Python String center()
Python Program to Slice Lists
Python help()
Python Machine Learning - Sebastian Raschka
Python String count()
Python String expandtabs()
Python Statement, Indentation and Comments
Python Program to Check If a List is Empty
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Machine Learning Eqution Reference - Sebastian Raschka