In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to make Python programs run faster". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to make Python programs run faster".
Timing and performance analysis
Before we start tuning, we first need to find out which part of the code is really slowing down the entire program. Sometimes the bottleneck in program performance is obvious, but when you don't know where the bottleneck is, here are some ways to help you find it:
Note: the following program is used for demonstration purposes, which calculates e to the power of X (extracted from the Python document):
# slow_program.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 exp (Decimal (150)) exp (Decimal (3000) exp (Decimal (3000))
The laziest performance analysis
First of all, the easiest way to be honest and lazy is to use Unix's time command:
~ $time python3.8 slow_program.py real 0m11058s user 0m11050s sys 0m0008s
If you just want to time the whole program, this command will do the job, but it's usually not enough.
The most detailed performance analysis
At the other extreme is cProfile, which provides "too much" 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 built-in method 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 find_spec 4 0.000 0.000 0.000 0. 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. 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 ()
Here, we run the test script with the cProfile module and time parameters to sort the output lines by internal time (cumtime). This gives us a lot of information, and the line you see above is only 10% of the actual output. From the output, we can see that the exp function is the culprit (surprise, surprise). Now we can focus more on timing and performance analysis.
Special function for timing
Now that we know what we need to focus on, we may just want to time slow functions without caring about the rest of the code. We can use a simple decorator to do this:
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.