In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to make the Python program run faster". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Python performance debugging
For Python performance, the prerequisite is to identify performance bottlenecks in the program. Find the code in the program that affects the performance of the program. Generally speaking, experienced developers can easily find the bottleneck of the program, but it is very difficult for ordinary programmers to find the problem code of the system. In order to find the performance bottleneck of the program quickly and effectively, we need to carry out performance debugging. Here we introduce with a practical example. The following program is to calculate the power of e to the power of x (1... n), and the code is as follows:
# performance.py from decimal import * def exp (x): getcontext (). Prec + = 2 I, lasts, s, fact, num = 0,0,1,1,1 while s! = lasts: lasts = s I + = 1 fact * = I num * = x s + = num / fact getcontext (). Prec-= 2 return + s print (exp (Decimal (3000)) print (exp (Decimal (3000)) print (exp (Decimal (3000)
The simplest debugging
The simplest and most practical way to debug performance debugging is to use the time command of Linux, where time can calculate the time it takes for a program to execute:
Time python3 performance.py 1.393709580666379697318341937E+65 5.221469689764143950588763007E+173 7.646200989054704889310727660E+1302 real 0m15.185s user 0m15.100s sys 0m0.004s
The calculation of the first two numbers (150400) is very fast, while the third one is very slow, which takes more than 15 seconds to complete, which is a bit stuttered (slow).
Time is convenient and useful, but it doesn't give us detailed code performance details.
Detailed performance analysis cProfile
Another common method of performance analysis is to use cProfile, which can provide a lot of performance information
Python3-m cProfile-s time performance.py
In the example, we used the cProfile module and the time parameter to run the test script to sort the rows by internal time (cumtime). As shown in the figure above, using cProfile can give a lot of internal specific information, from which we can know that the main time-consuming is caused by the exp function. Knowing where the performance bottleneck of the program lies, we will explain Python performance analysis and optimization.
Optimize specific featur
Knowing where the performance bottleneck lies (in this case, the exp function), in order to further analyze the specific problem, we use a simple decorator to skip other code and specifically analyze the functions designed by the performance bottleneck. Then test using the decorator, the specific code is as follows:
Def timeit_wrapper (func): @ wraps (func) def wrapper (* args, * * kwargs): start = time.perf_counter () # Alternatively, you can use time.process_time () func_return_val = func (* args, * * kwargs) end = time.perf_counter () print ('{0:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.