Python Program to Measure Elapsed Time
Overview
in this article, we shall learn about how to compute Elapsed time in Python. The time taken by an event i.e. the difference between the start and the end time of an event is called Elapsed time. Simply it can be defined as the passed time between the start and end of an event. We can calculate Elapsed time, by simply subtracting the end time and the start time. The formula to determine the Elapsed time is to subtract the hours and minutes separately. In this way, we can have the duration of the event.
Python elapsed Time Measurement
Now let's discuss the two terms - 'time' and 'elapse'. Elapse is a verb, it means 'to slip or pass by' and time is the progression or advancement of past events to the present and the future. So Elapsed time can be easily understood as the passed time from the start of an event till the end of it.
Let us see some daily life examples of Elapsed Time:
- The amount of time that a boy takes to finish up a race.
- The time elapsed to complete a process.
- The time taken by a train to complete a journey.
- Time is taken by the earth to complete a revolution around the sun.
In Python, by running a segment of code or a python script we can measure the Elapsed time by using some built-in python modules like time, DateTime, and time it.
Python Elapsed Time Measurement using Time Module
In Python's time module, there are multiple different methods to determine the Elapsed time of a code segment. Now we will cover the following methods of time module:
- time.perf_counter()
- time.time_ns()
- time.process_time
1. time.perf_counter()
This method of time module tracks the time in seconds. So to get it in a readable format, we just need to convert it into microseconds so as to get the time difference.
Code
Output
2. time.time_ns()
This method time.time_ns(), is used to calculate the execution time in nanoseconds. This method follows tracking the time before the code segment and after the code segment. Then subtract them and display them on the screen. In this way, it will calculate the time in nanoseconds instead of seconds.
Code
Output
3. time.process_time()
This module gives the Elapsed time by calculating the sum of the user and the CPU (Central Processing Unit) time and then returns it. This method also follows tracking the time before the code segment and after the code segment. Then subtract them and display them on the screen.
Code
Output
Python elapsed time measurement using DateTime module
To determine the execution time of a code segment, we can also use the Python Datetime module. The process is the same as using time.time(), first measuring the start time and end time and after it calculating the difference.
datetime.datetime.now()
Code
Output
TimeIt Module
To measure small code snippets, Python timeit module is often used. This function executes with an unknown function with several executions. While determining the execution time it turns off garbage collection but not permanently.
How to Use TimeIt Module
Now in the code below, we will see how this module works, and also we will implement it to determine the Elapsed time of a lambda expression. After importing the timeit module, we will determine the elapsed time by using the timeit() function from the module.
Code
Output
- Explanation Now the above piece of code will print "Hello World!" 10^6 times as the default parameter is 1000000. At last, this code will print the Elapsed time in seconds.
Using timeit.timeit() with Predefined Parameters
In the code below, we will write a block of python code that contains a function, and it's called a string and then after it, we will pass that string to timeit.timeit() function. To count the Elapsed time, we will specify the number of iterations.
Code
Output
- Explanation In the example above, a python function is defined and called, and the function is written as a string representation. For 10^6 times, we will test the running time and then we will display the amount of time taken in seconds to execute the function.
Using timeit.repeat()
This method helps us by saving us from the difficulty of storing values in an array and making a loop. This will help in getting the value of execution time from multiple executions of the same block of code. We use time.repeat() function in place of timeit.timeit() function. It takes a repeat parameter.
Code
Output
Using timeit.default_timer()
This method uses the time.perf_counter() to track the timestamp in nanoseconds of an instance, and then we can just subtract the end time and start time to get the duration of Elapsed time in nanoseconds.
Code
Output
Conclusion
- The time taken between the start and end of an event is called Elapsed time.
- The formula to determine the Elapsed time is to subtract the hours and minutes separately. In this way, we can have the duration of the event.
- Elapse is a verb, it means ‘to slip or pass by’ and time is the progression or advancement of past events to the present and the future.
- In Python, by running a segment of code or a python script we can measure the Elapsed time by using some built-in python modules like time, DateTime, and time it.
- The method time.perf_counter() of the time module tracks the time in seconds. So to get it in a readable format, we just need to convert it into microseconds so to get the time difference.
- This method time.time_ns(), is used to calculate the execution time in nanoseconds. This method follows tracking the time before the code segment and after the code segment. Then subtract them and display them on the screen.
- This module gives the Elapsed time by calculating the sum of the user and the CPU (Central Processing Unit) time and then returns it.
- To determine the execution time of a code segment, we can also use the Python Datetime module. The process is the same as using time.time(), first measuring the start time and end time and after it calculating the difference.
- To measure small code snippets, Python timeit module is often used. This function executes with an unknown function with several executions. While determining the execution time it turns off garbage collection but not permanently.