How to use Python time Module to measure elapsed time in Python In Python

In Python time module, there are different methods to record and find the execution time of a given code segment, we covered the usage of the following methods: time.perf_counter(), time.time_ns(), time.process_time(), time.time()

Example 1: How to measure elapsed time using time.perf_counter()

time.perf_counter() method records the time in seconds time unit. Since our sample function is very simple, so, we need to convert it to micro seconds to get time difference value in readable format.

Python3




# importing the module
import time
 
 
# function to test elapsed time for
def print_square(x):
    return x ** 2
 
 
# records start time
start = time.perf_counter()
 
# calls the function
print_square(3)
 
# record end time
end = time.perf_counter()
 
# find elapsed time in seconds
ms = (end-start) * 10**6
print(f"Elapsed {ms:.03f} micro secs.")


Output:

Elapsed 1.014 micro secs.

Example 2: How to measure elapsed time using time.time_ns()

To measure the elapsed time or execution time of a block of code in nanoseconds, we can use the time.time_ns() function. This follows the same syntax as the time.time() function, like recording the time before and after the lines of the code and then subtracting the values and then printing them to the screen, but it records in nanoseconds instead of seconds.

Python3




# importing the module
import time
 
# sample function for testing
def print_square(x):
    return (x**2)
 
# record start time
start = time.time_ns()
 
# calls the function
print_square(3)
 
# record end time
end = time.time_ns()
 
# printing elapsed time in nanoseconds
print("Time taken", end-start, "ns")


Output:

Time taken 2671 ns

Example 3: How to measure elapsed time using time.process_time()

time.process_time() function returns the sum of the system and the user CPU time. This follows the same syntax as the time.time() function, like recording the time before and after the lines of the code and then subtracting the values, and then printing them to the screen.

Python3




# importing the module
import time
 
 
# sample function for testing
def print_square(x):
    return x ** 76567
 
 
# record start time
start = time.process_time()
 
# calls the function
print_square(125)
 
# record end time
end = time.process_time()
 
# print elapsed time in seconds
print("Elapsed time using process_time()", (end - start) * 10**3, "ms.")


Elapsed time using process_time() 12.583209999999998 ms.

How to measure elapsed time in Python?

In Python, we can measure the elapsed time on executing a code segment or a Python script using some built-in Python modules. Here we will cover the usage of time, timeit and datetime module.

Similar Reads

Using Python timeit Module to measure elapsed time in Python

Python timeit module is often used to measure the execution time of small code snippets. We can also use the timeit() function, which executes an anonymous function with a number of executions. It temporarily turns off garbage collection while calculating the time of execution....

Using Python time Module to measure elapsed time in Python

...

Using Python datetime Module to measure elapsed time in Python

...