Using several built-in Python modules, we can determine how long it took to execute a section of code or a Python script in Python. In this article, we'll go through how to use the timeit, and datetime modules to determine elapsed time.
The Python timeit module is often used to calculate how long it takes for short pieces of code to run. We may also use the timeit() method, which repeatedly calls an anonymous procedure. While determining the execution time, trash collection is momentarily disabled.
Measuring Elapsed Time in Python using the timeit Module
The timeit
module in Python provides an easy way to measure the execution time of pieces of Python code. It provides programmers very much easy way to time the execution of a code snippet, whether it's a single line or a block of code.
Here are the steps to use the timeit
module in Python:
- Import the
timeit
module: To use the timeit
module, you need to first import it in your Python script. You can do this by using the following code:
import timeit
- Define the code to be timed: You need to specify the code that you want to measure the execution time of. You can do this by wrapping it in a string, and passing it as an argument to the
timeit
function. For example:
code_to_be_timed = """
# code to be timed
a = [i for i in range(100000)]
"""
-
Call the timeit function: To measure the execution time of the code, you need to call the timeit.
Measuring Elapsed Time in Python using the timeit.timeit()
with Predefined Parameters
The timeit.timeit()
function is used to time the execution of a single statement and is executed a number of times to reduce measurement noise. By default, the timeit.timeit()
function runs the statement 7 times and provides the best of 3 runs.
You can also provide predefined parameters to the timeit.timeit()
function by specifying the number
and repeat
arguments. The number
argument specifies the number of times the statement should be executed per loop, and the repeat
argument specifies the number of times to repeat the measurement.
For example, if you wanted to measure the execution time of a simple statement, you could use the following code:
import timeit
statement = "x = [i**2 for i in range(10000)]"
execution_time = timeit.timeit(statement, number=1000, repeat=3)
print("Execution time:", execution_time, "seconds")
In this example, the statement x = [i**2 for i in range(10000)] is executed 1000 times per loop and repeated 3 times, providing an average execution time.
Measuring Elapsed Time in Python using timeit.repeat()
The timeit.repeat()
function in Python provides a more easy way to measure the execution time of a statement with more control over the number of loops and repeats. Unlike the timeit.timeit()
function, timeit.repeat()
returns a list of times for each repeat, allowing you to see the distribution of times rather than just the average.
You can use the timeit.repeat()
function to measure the elapsed time of a statement by providing the stmt
argument with the statement to be executed and the repeat
argument with the number of times to repeat the measurement. You can also provide the number
argument with the number of loops to run the statement in each repeat.
Here is an example of how you could use timeit.repeat()
to measure the elapsed time of a simple statement:
import timeit
statement = "x = [i**2 for i in range(10000)]"
elapsed_times = timeit.repeat(stmt=statement, repeat=5, number=1000)
print("Elapsed times:", elapsed_times)
In this example, the statement x = [i**2 for i in range(10000)]
is executed 1000 times per loop and repeated 5 times. The timeit.repeat()
function returns a list of elapsed times, one for each repeat, which you can use for further analysis.
Measuring Elapsed Time in Python using timeit.default_timer() Module
The timeit
module in Python provides a simple way to measure the execution time of small bits of Python code. The default_timer
function in timeit
provides a platform-independent timer to measure the elapsed time.
Here's an example usage of timeit.default_timer
to measure the elapsed time of a Python code block:
import timeit
start = timeit.default_timer()
# Your code block here
elapsed = timeit.default_timer() - start
print("Elapsed time: ", elapsed)
The start variable is set to the current time before the code block starts, and elapsed is calculated as the difference between the current time and start after the code block finishes. The elapsed time is then printed.
Conclusion
In conclusion, measuring elapsed time in Python can be done using the timeit module. The default_timer function provides a platform-independent way to measure the elapsed time of a code block. To use default timer, set a variable to the current time before the code block starts and calculate the difference between the current time and the starting time after the code block finishes. The elapsed time can then be printed or used for further calculations. The timeit module also provides other functions that can be used to measure the execution time of code, so it's a good idea to consult the documentation for more information.