How to write a function to get the time spent in each function in Python?



In Python, measuring the execution time of a function or block of code can be done using several functions and methods provided by built-in modules like time and datetime. The following are the methods to measure the execution time of Python functions.

Using time.time() Method

The time.time() method (returns the current UTC) can measure how long a function takes to run. It returns the current time in seconds since January 1, 1970 (known as the Unix epoch). To measure the execution time of a function, we need to use time.time() before the function starts and again after it finishes.

By subtracting the start time from the end time, we get the total time spent on executing the function as a floating-point value.

Example

The following program returns the time taken for program execution by using the time.time() method:

import time

starttime = time.time()
print("Hello tutorialspoint python codes")

# getting the time taken for executing the code in seconds
endtime = time.time()

# Printing the time taken for code execution
print("Time taken for code execution:", endtime - starttime)

Following is the output of the above code:

Hello tutorialspoint python codes
Time taken for code execution: 0.0003402233123779297

Using time.process_time() Function

The time.process_time() function measures the CPU execution time of a program, which means it only counts the time spent on the CPU running our code (excluding sleep time or time spent waiting for input/output).

To measure how long a function takes to run by using this approach, we need to call this function before the function starts to record the start time, and after the function finishes running, call the time.process_time() again to get the end time. By subtracting the start time from the end time, we get the CPU time used by that function.

Example

In the following program, we are going to calculate the CPU time taken to find the maximum element in a list by using the time.process_time() function:

import time
starttime = time.process_time()

inputList = [2, 5, 9, 1, 6]
maxElement = max(inputList)

# Printing maximum element of the list
print('Maximum Element in input list: ', maxElement)

# getting the time taken for executing the code
endtime = time.process_time()

executionTime = endtime - starttime
print('CPU Execution time:', executionTime)

Following is the output of the above code:

Maximum Element in input list: 9
CPU Execution time: 3.561200000000084

Using datetime.now() Function

The now() function from the datetime module will return the current local time in YYYY-MM-DD HH:MM:SS.microseconds format. It can also be used to measure how long a function takes to run by following the same steps as in previous methods.

When we subtract the start time from the end time, it returns a timedelta object, which represents the duration or difference between two times, in HH:MM:SS format.

Example

In the following program, we record the current time before and after counting words in a string using the datetime.now() function. Then we subtract the start time from the end time to get the total execution time as a timedelta object (result) in HH:MM:SS format.

from datetime import datetime

starttime = datetime.now()

sampleText = "Python programming is fun"
wordCount = len(sampleText.split())
print('Number of words in text:', wordCount)

endtime = datetime.now()

executionTime = endtime - starttime
print('Execution Time:', executionTime)

Following is the output of the above code:

Maximum Element in input list: 9
Execution Time: 0:00:00.002986
Updated on: 2025-08-28T11:26:06+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements