Many times, it is necessary to understand the efficiency and memory usage of Python programs in order to optimize implementation. The following are several commonly used methods.
1. Use print.
You can load the datatime module and print datatime. datatime. now() where you need it, so you can know the time point when a certain line of code is executed. You can also use a decorator to output the time.
2. Use the time command in Linux.
Command: time - p Python hotspot_Client_The output result of trend. py is:
Completed time: 2017-01-18 09:45:46.997727
real 8.10
user 4.56
sys 1.55.
The real time is usually longer than the program execution time.
3. Guppy.
The installation of GUPPY is very simple, pip install puppet. After installation is completed, load the module:
From guppy import hpy.
Then, after the required code, call hpy(). heap() to print out the memory usage information. If more information is needed, you can use heap(). more(), heap(). more(), and so on.
4. Line_Profiler.
This module can provide the time spent on each line of code.
After module installation, there is no need to load the module in the code, just specify the use of @Detect which function using profile. Then execute the program using the following command.
Kernprop - l - v hotspot_Client_Trend.py.
The parameter - l represents line by line, and - v represents outputting detailed results.