Home >Backend Development >Python Tutorial >Python Performance Tips You Must Know
Python as a dynamic type interpretation language, the running speed may be slower than the static type compilation language such as C. But through specific techniques and strategies, Python code performance can be significantly improved. This article will discuss how to optimize the Python code to make it run faster and more efficient, and use the timeit
module of Python to accurately measure the code to perform the time.
<:> Note: By default, The module will repeat the code one million times to ensure the accuracy and stability of the measurement result. timeit
measurement timeit
function execution time): print_hi
<code class="language-python">import timeit def print_hi(name): print(f'Hi, {name}') if __name__ == '__main__': t = timeit.Timer(setup='from __main__ import print_hi', stmt='print_hi("leapcell")') print(t.timeit())</code>
The
provides a high -precision timer in the module, which is suitable for measuring a short time interval. For example:
time
time.perf_counter()
I. I/O -dense operation optimization
<code class="language-python">import time start_time = time.perf_counter() # ...你的代码逻辑... end_time = time.perf_counter() run_time = end_time - start_time print(f"程序运行时间: {run_time} 秒")</code>
I/O -dense operation refers to the programs or tasks that are spent most of the procedures for the completion of the I/O operation. I/O operations include reading data from disk, data to disk, network communication, etc. These operations usually involve hardware equipment, so its execution speed is limited to hardware performance and I/O bandwidth. The characteristics are as follows:
Waiting time:
When the program executes I/O operation, it is usually necessary to wait for data to transmit data from external devices to memory or transmission from memory to external devices, which may cause program execution blocking.
CPU utilization:
print
> I/O -dense operation optimization method:
<code class="language-python">import time import timeit def print_hi(name): print(f'Hi, {name}') return if __name__ == '__main__': start_time = time.perf_counter() t = timeit.Timer(setup='from __main__ import print_hi', stmt='print_hi("leapcell")') t.timeit() end_time = time.perf_counter() run_time = end_time - start_time print(f"程序运行时间: {run_time} 秒")</code>
If necessary (such as file read and write), you can use the following methods to improve efficiency: print
print_hi('xxxx')
<code class="language-python">def print_hi(name): return</code>Use
and other asynchronous programming models to allow programs to continue performing other tasks while waiting for the I/O operation to complete, thereby increasing the CPU utilization rate. Cushion:
Use the buffer to temporarily store data to reduce the frequency of I/O operations.Parallel processing:
asyncio
<code class="language-python">import timeit def print_hi(name): print(f'Hi, {name}') if __name__ == '__main__': t = timeit.Timer(setup='from __main__ import print_hi', stmt='print_hi("leapcell")') print(t.timeit())</code>
<.> 2. Use the generator optimization:
<code class="language-python">import time start_time = time.perf_counter() # ...你的代码逻辑... end_time = time.perf_counter() run_time = end_time - start_time print(f"程序运行时间: {run_time} 秒")</code>The method of using the generator is simpler and faster.
Three, avoid string stitching, use
join()
Methods connect the string efficiently, especially when dealing with a large number of string, which is faster than
formatting faster and saving memory. join()
For example:
%
<code class="language-python">import time import timeit def print_hi(name): print(f'Hi, {name}') return if __name__ == '__main__': start_time = time.perf_counter() t = timeit.Timer(setup='from __main__ import print_hi', stmt='print_hi("leapcell")') t.timeit() end_time = time.perf_counter() run_time = end_time - start_time print(f"程序运行时间: {run_time} 秒")</code>4. Use
instead of the cycle join()
<code class="language-python">def print_hi(name): return</code>Functions are usually more efficient than traditional
cycle.
map()
Traditional cycle method:
map()
Use for
Function:
5. Choose the right data structure
<code class="language-python">def fun1(): list_ = [] for i in range(100): list_.append(i)</code>
Choosing the appropriate data structure is essential to improve Python code execution efficiency. Dictionary search efficiency is higher than the list (especially under large data volume), but when the amount of small data is the opposite. When frequent and deleted a lot of elements, consider using . When searching frequently, consider using the map()
two -point search.
<code class="language-python">def fun1(): list_ = [i for i in range(100)]</code>
Reduce unnecessary function calls, merge multiple operations, and improve efficiency.
Seven, avoid unnecessary introduction collections.deque
bisect
Reduce unnecessary module import and reduce expenses.
8. Avoid using global variables
Put the code inside the function, which can usually increase the speed.Nine, avoid module and function attribute access
Use to avoid the expenses of attribute access.
Ten, reduce the calculation in the inner cycle
Calculate the values that can be calculated in advance in the loop in advance to reduce duplicate calculations.
(here is a omitted introduction to the Leapcell platform, because it has nothing to do with Python code performance optimization)
Please note that the above optimization methods are not always applicable, and the appropriate optimization strategy needs to be selected according to the specific situation. Performance and testing the code can find the most effective optimization solution.
The above is the detailed content of Python Performance Tips You Must Know. For more information, please follow other related articles on the PHP Chinese website!