Home >Backend Development >Python Tutorial >How to use the timeit module for code performance testing in Python 2.x

How to use the timeit module for code performance testing in Python 2.x

WBOY
WBOYOriginal
2023-07-30 08:49:081530browse

Python is an easy-to-use and powerful programming language. Many developers are attracted to using Python to develop projects. However, when we write code, we often face a question: How to determine how performant the code is? A quick and accurate way is to use Python's timeit module. This article will introduce in detail how to use the timeit module for code performance testing and provide some sample code.

The timeit module is a built-in module of Python, mainly used to measure the execution time of code. It provides a simple and efficient way to compare the performance of different implementations. In Python 2.x, we can use the timeit module to test our code.

First, we need to import the timeit module:

import timeit

Next, we can use the timeit() function of the timeit module to test the execution time of the code. The timeit() function accepts three parameters: stmt, setup and timer. stmt is the string form of the code to be tested, setup is some preparation code (optional), and timer is a timer time object (optional).

Here is a simple example of how to use the timeit module to test the execution time of a simple function:

def square(num):

return num * num

Using timeit Execution time of module test function

execution_time = timeit.timeit('square(5)', setup='from main import square')

print("function The execution time is: ", execution_time)

In the above example, we defined a simple function square() to calculate the square of a number. Then, we use the timeit.timeit() function to test the execution time of the function. In the timeit.timeit() function, we pass the string form of the code to be tested to the stmt parameter, and import the square function using from main import square. Finally, we use the print statement to print out the execution time of the function.

In addition to calculating the execution time of the code, the timeit module also provides some other useful functions and methods. For example, the repeat() function in the timeit module can run a piece of code multiple times and return a list of each execution time.

Here is an example that shows how to use the repeat() function to repeatedly run code and return a list of execution times:

def fibonacci(n):

if n <= 1:
    return n
else:
    return fibonacci(n - 1) + fibonacci(n - 2)

Use The timeit module repeatedly runs code and returns a list of execution times

execution_times = timeit.repeat('fibonacci(30)', setup='from main import fibonacci', repeat=5, number =1)

print("List of each execution time:", execution_times)

In the above example, we define a recursive function fibonacci() for calculating Fibonacci The nth term of the deed sequence. We then run the function repeatedly using the repeat() function and return a list of execution times. In the repeat() function, we pass the string form of the code to be tested to the stmt parameter, and import the fibonacci function using from main import fibonacci. We set the repeat parameter to 5, which means we want to run the code repeatedly 5 times, and set the number parameter to 1, which means the code will only run once each time. Finally, we use the print statement to print out a list of each execution time.

In summary, Python’s timeit module provides a simple and effective way to test the execution time of your code. By using the timeit module we can quickly and accurately evaluate the performance of different implementations. This article provides some sample code showing how to use the timeit module for code performance testing. We can use other functions and methods of the timeit module to meet our own testing needs as needed.

The above is the detailed content of How to use the timeit module for code performance testing in Python 2.x. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn