In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the ways to improve the performance of Python". 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!
Time series analysis
Before you optimize, first find out which part of the code is slowing down the entire program. Sometimes the "bottleneck" of the program is not obvious, if you can't find it, here are some suggestions for reference:
Note: this is a demo program for calculating the x power of e (from the Python document):
# slow_program.py from decimal import* defexp (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 exp (Decimal (150) exp (Decimal (400)) exp (Decimal (3000))
View all the rawslow_program.py code on GitHub
The most labor-saving "performance analysis"
First, the simplest and most labor-saving solution is to use Unix's time command:
~ $time python3.8 slow_program.py real 0m11058s user 0m11050s sys 0m0008s
View all the rawbase_time.shell code on GitHub
If you just time the whole program, it's useful, but it's not enough.
The most detailed performance analysis
Another method of performance analysis is cProfile, from which you can get a large amount of information:
~ $python3.8-m cProfile-s time slow_program.py 1297 function calls (1272 primitive calls) in 11.081 seconds Ordered by: internal time ncalls tottime percall cumtime percall filename:lineno (function) 3 11.079 3.693 11.079 3.693 slow_program.py:4 (exp) 1 0.000 0.000 0.002 0.002 {built-in method _ imp .create _ dynamic} 4 abc.py:132 1 0.000 0.000 11.081 11.081 {built-in method builtins.exec} 6 0.000 0.000 0.000 {built-in method _ _ new__ of type object at 0x9d12c0} 6 0.000 0.000 0.000 abc.py:132 (_ _ new__) 23 0.000 0.000 0.000 _ weakrefset.py:36 (_ _ init__) 245 0.000 0.000 0.000 {built-in method builtins.getattr} 2 0.000 0.000 0.000 {built-in method marshal.loads} 10 0.000 0.000 0.000: 1233 (find_spec) 8 abc.py:196 4 0.000 0.000 0.000 abc.py:196 (_ _ subclasscheck__) 15 0.000 0.000 0.000 {built-in method posix.stat} 6 0.000 0.000 0.000 {built-in method builtins.__build_class__} 1 0.000 0.000 0.000 _ init__.py:357 (namedtuple) 48 0.000 0.000 0.000: 57 (_ path_join) 48 0.000 0.000 0.000: 59 () 1 0.000 0.000 11.081 11.081 slow_program.py:1 ()
View all the rawcprofile.shell code on GitHub
Here you run the test script with the cProfile module and time parameters to sort the rows by internal time (cumtime). You can get a lot of information from this, and the results listed above are about 10% of the actual output. Thus it can be seen that the exp function is the "culprit" of slowing down the program (amazing! Now look at a more detailed timing and performance analysis.
Timing a specific function
Now that you know the function that slows down the program, the next step is to use a simple modifier to time the function without measuring the rest of the code. As follows:
Deftimeit_wrapper (func): @ wraps (func) defwrapper (* args, * * kwargs): start = time.perf_counter () # Alternatively, you can use time.process_time () funcfunc_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: 294
*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.