
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to measure elapsed time in python?
To measure time elapsed during program's execution, either use time.clock() or time.time() functions. The python docs state that this function should be used for benchmarking purposes.
example
import time t0= time.clock() print("Hello") t1 = time.clock() - t0 print("Time elapsed: ", t1) # CPU seconds elapsed (floating point)
Output
This will give the output −
Time elapsed: 1.2999999999999123e-05
You can also use the time module to get proper statistical analysis of a code snippet's execution time. It runs the snippet multiple times and then it tells you how long the shortest run took. You can use it as follows:
Example
def f(x): return x * x import timeit timeit.repeat("for x in range(100): f(x)", "from __main__ import f", number=100000)
Output
This will give the output −
[2.0640320777893066, 2.0876040458679199, 2.0520210266113281]
- Related Articles
- How to measure elapsed time in Java?
- How to measure elapsed time in nanoseconds with Java?
- How to calculate elapsed/execution time in Java?
- How to calculate Elapsed time in OpenCV using C++?
- How to measure time with high-precision in Python?
- Get elapsed time in Java
- Measuring elapsed time in Java
- Compute elapsed time in seconds in Java
- Get elapsed time in minutes in Java
- Compute elapsed time in hours in Java
- Get elapsed time in days in Java
- How to measure the execution time in Golang?
- How to measure actual MySQL query time?
- How do we measure time?
- Compute the elapsed time of an operation in Java

Advertisements