how to measure elapsed time in python

How to Measure Elapsed Time in Python

Python is a powerful language that allows you to do tons of things. Often you may need to measure the amount of time taken to execute your script or code. There are many python modules to help you do this. In this article, we will learn how to measure elapsed time in Python. In this article, we will learn how to measure elapsed time in python using timeit module.


How to Measure Elapsed Time in Python

We will learn how to measure elapsed time in python.


1. Using timeit module

Timeit module is used to measure execution time of small code snippets and scripts. It turns off garbage collection while measuring execution time. Let us look at a few examples to measure elapse time.

First, we will learn how to use timeit module to find time takes to execute the function and print it as output. In this case, we simply import timeit module, and call timeit.timeit() function to measure the time taken to execute lambda function. Its output is stored in variable t, as number of seconds. We finally print this value.

# importing the module
import timeit

# using the timeit method and lambda
# expression to get the execution time of
# the function.
t = timeit.timeit(lambda: "print('Hello World!')")

# printing the execution time
print(t)

When you run the above code, you will see output as shown below.

0.0577151

Next, we will measure the execution time of a function by calling it in timeit.timeit() function.

# importing the module
import timeit

# sample function that returns square
# of the value passed
def print_square(x):
	return (x**2)

# using the timeit method and lambda
# expression to get the execution time of
# the function, number defines how many
# times we execute this function
t = timeit.timeit(lambda: print_square(3), number=10)

# printing the execution time
print(t)

When you run the above code, you see the output as shown below.

4.499999999999749e-06

In the above code, we import timeit library, and define a sample function print_square() that calculates the square of input value. In timeit.timeit() function we pass two arguments – function call with input parameters, and the number of times you want this function to be called. The second parameter is optional. If you omit it, timeit.timeit() function will execute the function once. In this case also, timeit.timeit() function returns the total number of seconds taken to execute all the function calls, which we print as output.

Typically, people run a code multiple times, measure the elapsed time for each instance and take their average to determine the execution time of a code. timeit.repeat() function allows you to repeat your function calls and measure the execution time. It repeats the function calls multiple times, stores execution time of each instance in an array. Here is an example.

# importing the module
import timeit


# sample function that returns square
# of the value passed
def print_square(x):
	return (x**2)

# using the repeat method and lambda
# expression to get the execution time of
# the function, number defines how many
# times we execute this function and the
# repeat defines the number of times the
# time calculation needs to be done.
t = timeit.repeat(lambda: print_square(3), number=10, repeat=5)

# printing the execution time
print(t)

In the above code, we pass 3 parameters to timeit.repeat() function – function call with input parameters, number of times to repeat the function call, and the number of times to repeat this exercise. So in out example, timeit.repeat() function will call print_square(3) 10 times and measure the total execution time of these 10 function calls. It will repeat this exercise 4 more times, that is, total 5 times, and measure separate execution times for executing print_square() 10 times each. The output of timeit.repeat() function is an array and not a single variable.

When you run the above code, you will see the output as an array of execution times.

[3.800000000000249e-06, 2.299999999997749e-06, 4.2000000000018125e-06, 4.1999999999948736e-06,2.4000000000006247e-06]

You can also use timeit.default_timer() function to record the time at which a particular line is executed. You can call this just before and after the lines whose execution time you want to measure, and calculate the difference between the two values to calculate execution time. Here is an example.

# importing the module
import timeit


# sample function that returns
# square of the value passed
def print_square(x):
	return (x**2)

# records the time at this instant
# of the program
start = timeit.default_timer()

# calls the function
print_square(3)

# records the time at this instant
# of the program
end = timeit.default_timer()

# printing the execution time by subtracting
# the time before the function from
# the time after the function
print(end-start)

In the above example, we use timeit.default_timer() function before and after the call to print_square() function.

In this article, we have learnt how to calculate elapsed time in python.

Also read:

How to Create ISO Image from CD in Linux
How to Export Pandas Dataframe to Multiple Excel Sheets
How to Export Pandas Dataframe to Excel
How to Export Pandas Dataframe to PDF
How to Run Python Script in Apache Server

Leave a Reply

Your email address will not be published. Required fields are marked *