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 Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python pow()
Python Program to Solve Quadratic Equation
Python __import__()
APIs in Node.js vs Python - A Comparison
Python dir()
Python String capitalize()
Python List Comprehension
Python Custom Exceptions
Python Program to Check Prime Number
Python Dictionary get()
Intelligent Projects Using Python - Santanu Pattanayak
Python Program to Count the Number of Occurrence of a Character in String
How to Get Started With Python?
Python Iterators
Python time Module
Python Strings
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Swap Two Variables
Python ord()
Python Statement, Indentation and Comments
Python Program to Get Line Count of a File
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python memoryview()
Python Program to Calculate the Area of a Triangle
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Machine Learning with Python for everyone - Mark E.Fenner
Python Dictionary pop()
Python Matrices and NumPy Arrays
Python Set pop()