In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
The knowledge of this article "python function running memory time and other performance testing tools how to use" is not quite understood by most people, so the editor gives you a summary of the following contents, detailed contents, clear steps, and a certain reference value. I hope you can get something after reading this article, let's take a look at this "python function running memory time and other performance testing tools how to use" article.
Basic test function
First, write a basic python function for various performance tests later.
Def base_func (): for n in range (10000): print ('the value of current n is: {}' .format (n)) memory_profiler process
Memory_profiler is a non-standard library for python, so pip is used to install it here. It can monitor processes, understand memory usage, and so on.
Pip install memory_profiler
After installing the memory_ profiler library, test it directly using annotations.
From memory_profiler import profile@profiledef base_func1 (): for n in range (10000): print ('the value of current n is: {}' .format (n)) base_func1 () # Line # Mem usage Increment Occurrences Line Contents# = # 28 45.3 MiB 45.3 MiB 1 @ profile# 29 def base_func (): # 30 45.3 MiB 0.0 MiB 10001 for n in range (10000): # 31 45.3 MiB 0.0 MiB 10000 print ('current n value is: {}' .format (n))
Judging from the data returned, the execution of the current function uses 45.3 MiB of memory.
Timeit time usage
Timeit is a built-in module of python that can test the running time of a cell's code. Because it is a built-in module, it does not need to be installed separately.
Import timeitdef base_func2 (): for n in range (10000): print ('the value of the current n is: {}' .format (n)) res = timeit.timeit (base_func2,number=5) print ('the running time of the current function is: {}' .format (res)) # the running time of the current function is: 0.967580099999993
According to the result of the above function, the running time of the function is 0.96 seconds.
Line_profiler line code detection
If you only need to detect the local run time of the function, you can use line_profiler, which can detect the run time of each line of code.
Line_profiler is a non-standard library for python. Install it using pip.
Pip install line_profiler
The easiest way to use it is to add the functions that need to be tested directly.
Def base_func3 (): for n in range (10000): print ('the value of current n is: {}' .format (n)) from line_profiler import LineProfilerlp = LineProfiler () lp_wrap = lp (base_func3) lp_wrap () lp.print_stats () # Line # Hits Time Per Hit% Time Line Contents# = # 72 def base _ func3 (): # 73 10001 162738.0 16.3 for n in range (10000): # 74 10000 3207772.0 320.8 95.2 print ('the current value of n is: {}' .format (n))
From the running results, you can see the running time and proportion of each line of code, and note that the unit of time here is subtle.
Heartrate visual inspection
The most recommended thing about heartrate is that it can be used to detect the execution of programs like heart rate on the web. At the same time, it is also a non-standard library and installed using pip.
Pip install heartrateimport heartrateheartrate.trace (browser=True) def base_func4 (): for n in range (10000): print ('the current n value is: {}' .format (n))
After running, the console prints the following log:
# * Serving Flask app "heartrate.core" (lazy loading) # * Environment: production# WARNING: This is a development server. Do not use it in a production deployment.# Use a production WSGI server instead.# * Debug mode: off
And automatically open the browser address: http://127.0.0.1:9999
The above is about the content of this article on "how to use performance testing tools such as python function running memory time". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
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.