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:
Deep Learning with Python - Francois Chollet
Python Program to Sort a Dictionary by Value
Python print()
Python Numbers, Type Conversion and Mathematics
Python Program to Sort Words in Alphabetic Order
Python id()
Python Namespace and Scope
Python String center()
Python list()
Python Dictionary
Python Program to Get File Creation and Modification Date
Python String isidentifier()
Python String rpartition()
Python Program to Count the Number of Each Vowel
Python List insert()
Python Program to Delete an Element From a Dictionary
Python Closures
Python any()
Python Deep Learning Cookbook - Indra den Bakker
Python type()
Python bytes()
Python Program to Get a Substring of a String
Python String find()
Python Program to Convert Kilometers to Miles
Python float()
Python break and continue
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Dictionary pop()
Python bin()
Python globals()
Python Decorators
Python String index()