How to measure elapsed time in Python?In Python, there are several modules that help you to find the execution time of a program. Measuring the execution time of a program depends on your Operating System, Python version, and what you mean by "time".
Using time module in PythonThe time() function returns the number of seconds passed since epoch. The time.time() function of Time module is used to get the time in seconds since epoch. The handling of leap seconds is platform dependent. You can use time.time() to measure the elapsed wall-clock time between two points:
import time startTime = time.time() print("Program running....") endTime = time.time() print(endTime - startTime)
Output: Program running.... 4.0531158447265625e-06In the above code, the difference between the endTime and startTime, which gives the execution time. It is important to note that, the execution time depends on the Operating System.
Measuring Python execution time in seconds
Python datetime.timedelta is a duration expressing the difference between two date, time, or datetime instances to microsecond resolution. With timedelta you can add days, minutes, seconds, hours, weeks and more to a datetime.date, or a datetime.datetime object. In the following program, you can measure execution time in seconds using timedelta.
import time from datetime import timedelta startTime = time.time() print("Program running....") endTime = time.time() print(endTime - startTime) print("Execution time in seconds :") print(timedelta(seconds=endTime-startTime))
Output: Program running.... 3.5762786865234375e-06 Execution time in seconds : 0:00:00.000004
- On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of "processor time", depends on that of the 'C' function of the same name.
- On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond.
Using timeit module in PythonPython timeit module provides a simple way to find the execution time of small bits of Python code. It avoids a number of common traps for measuring execution times. This module runs your snippet of code millions of times (default value is 1000000) so that you get the statistically most relevant measurement of code execution time.
import timeit executionTime = timeit.timeit(lambda: "print('Hello World!')") print(executionTime) //output:0.26035445800516754
timeit.default_timerAlso you can use timeit.default_timer instead of timeit.timeit. The default_timer provides the best clock available on your platform and version of Python automatically:
from timeit import default_timer as timer startTime = timer() print("Program running....") endTime = timer() print(endTime - startTime)
Output: Program running.... 3.978959284722805e-06The default_timer() measurement can be affected by other Python programs running on the same machine, so the best thing to do when accurate timing is necessary is to repeat the timing a few times and use the best time. On Unix, you can use time.clock() to measure CPU time.
Using Datetime ModuleDatetime module can also be used to find the time elapsed or spent in a code block provided you are not looking for high precision. The program return in HH:MM:SS format.
from datetime import datetime startTime = datetime.now() print("Program running....") endTime = datetime.now() print(endTime - startTime)
Output: Program running.... 0:00:00.000006
Elapsed Time as Days, Hours, Minutes and Seconds
from datetime import datetime as dt startTime = dt.fromtimestamp(1588432670) endTime = dt.now() elapsed=endTime-startTime print("Elapsed Time : %02d:%02d:%02d:%02d" % (elapsed.days, elapsed.seconds // 3600, elapsed.seconds // 60 % 60, elapsed.seconds % 60))
Output: Elapsed Time : 817:15:37:50
- How to use Date and Time in Python
- Python Exception Handling
- How to Generate a Random Number in Python
- How to pause execution of program in Python
- How do I parse XML in Python?
- How to read and write a CSV files with Python
- Threads and Threading in Python
- Python Multithreaded Programming
- Python range() function
- How to Convert a Python String to int
- Python filter() Function
- Difference between range() and xrange() in Python
- How to print without newline in Python?
- How to remove spaces from a string in Python
- How to get the current time in Python
- Slicing in Python
- Create a nested directory without exception | Python
- How to concatenate two lists in Python
- How to Find the Mode in a List Using Python
- Difference Between Static and Class Methods in Python?