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


In this article, we will show you how to write a function to get the time spent in each function using python. Now we see 4 methods to accomplish this task−

Now we see 2 methods to accomplish this task−

  • Using time.clock() function

  • Using time.time() function

  • Using time.process_time() function

  • Using datetime.now() function

Method 1: Using time.clock()

Python's Time module provides a variety of time−related functions.

The method time.clock() returns the current processor time as a floating point number expressed in seconds on Unix. The precision depends on that of the C function of the same name, but in any case, this is the function to use for benchmarking Python or timing algorithms.

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.

Syntax

time.clock() 

Parameters

  • Return Value- Returns a float containing the current processor time in seconds.

  • Algorithm (Steps)

    Following are the Algorithm/steps to be followed to perform the desired task −

    • Use the import keyword, to import the time module.

    • Get the current time before the code execution using the clock() method (Pythom time method clock() returns the current processor time as a floating point number expressed in seconds on Unix) and store this in a variable this will be the starting time for the code execution.

    • Write some sample code.

    • Get the current time once more. This is the time after which the code is executed, and it is saved as the end time when the code execution is completed, and it is saved in a variable.

    • Subtract the end time with the start time to get the time elapsed of the code and print it.

    Example

    The following program returns the time taken for code execution using the time.clock() function −

    # importing time module import time # Get the current time before executing the code starttime = time.process_time() # code print("Hello tutorialspoint python codes") # getting the time taken for executing the code in seconds endtime = time.process_time() # Printing the time taken for code execution print("Time Taken for code execution is:", endtime - starttime)

    Output

    On executing, the above program will generate the following output −

    Hello tutorialspoint python codes
    Time Taken for code execution is: 0.00230999999999959
    

    Method 2: Using time.time() function

    Algorithm (Steps)

    Follow the same steps as the previous method but use the time() function(returns the time as a floating point number expressed in seconds since the time module's epoch, in UTC) to get the time.

    Example

    The following program returns the time taken for code execution using the time.time() function −

    # importing time module import time # Get the current time before executing the code starttime = time.time() # code 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)

    Output

    On executing, the above program will generate the following output −

    Hello tutorialspoint python codes
    Time taken for code execution: 0.019986867904663086
    

    Method 3: Using time.process_time() function

    Follow the same steps as the previous method but use the process_time() function(It is used to calculate the CPU execution time of code. If you wish to measure a program's CPU execution time, use time.process_time() rather than time.time()) to get the time.

    Example

    The following program returns the time taken for code execution using the time.process_time() function −

    import time # Get the current time before executing the code starttime = time.process_time() # code # Taking sample list inputList = [2, 5, 9, 1, 6] # Finding maximum element of the list 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() # Printing the time taken for code execution executionTime = endtime - starttime print('CPU Execution time:', executionTime)

    Output

    On executing, the above program will generate the following output −

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

    Here we performed finding the maximum element of the given list and we checked the elapsed time for executing that code using the process_time() function.

    Method 4: Using datetime.now() function

    Python includes a DateTime module for working with dates and times. DateTime is an inbuilt module in Python rather than a primitive data type; we simply need to import the module mentioned above to work with dates as date objects.

    Follow the same steps as the previous method but use the datetime.now() function(returns the current local date and time) to get the current date and time in HH:MM:SS format.

    Example

    The following program returns the time taken for code execution using the datetime.now() function −

    # importing datetime module from datetime import datetime # getting the current time in HH:MM:SS format starttime = datetime.now() # code # Taking sample list inputList = [2, 5, 9, 1, 6] # Finding maximum element of the list 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 = datetime.now() # Printing the time taken for code execution executionTime = endtime - starttime print('Execution Time:', executionTime)

    Output

    On executing, the above program will generate the following output −

    Maximum Element in input list: 9
    Execution Time: 0:00:00.002986
    

    It returned the time elapsed in HH:MM:SS format, indicating that the code was executed in 0.002986 seconds.

    Conclusion

    We learned how to get the time spent by the code using four different methods in this article. We used two types of examples to demonstrate the distinction between the time elapsed of two different codes.

Updated on: 22-Sep-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements