Profile :
Pycharm provides profiling Tools run-profile, as shown in the following figure. The profile tool can be used to analyze the code performance and find the bottleneck.
Test:
Here is a section of the test code to illustrate how to use the Pycharm profile feature.
Test code see below, the file named test.py, a total of 5 functions, each function called Time.sleep for delay, where the FUN5 function called the FUN4 function:
Time
fun1 (A, b):
print (' fun1 ')
print (A, b)
time.sleep (1)
fun2 ():
Print (' fun2 ')
time.sleep (1)
fun3 ():
print (' fun3 ')
time.sleep (2)
fun4 ():
print (' Fun4 ')
time.sleep (1)
fun5 ():
print (' fun5 ')
time.sleep (1)
Fun4 ()
fun1 (' foo ', ' Bar ')
fun2 ()
fun3 ()
fun5 ()
Click run-"Profile" to start the test, the code after the run will generate a column of test results, the test results are composed of two parts, Statistcs (performance statistics) and call graph (Invoke diagram):
Statistcs (Performance statistics):
The performance statistics interface consists of a table of name, call Count, Time (ms) and Own Time (MS) 4 columns, as shown in the following figure.
1. The header name shows the module or function being invoked; Call count shows the number of times it was invoked; time (ms) displays the elapsed time and time percentage, in milliseconds (ms).
2. Click on the small triangle on the head to arrange the table in ascending or descending order.
3. Double-click a row in the Name column to jump to the corresponding code.
4. In Fun4 This line example: Fun4 was called once, the running time is 1000ms, the total running time of 16.7%
Call graph (Invoke diagram):
The call Graph interface visually shows the direct invocation relationships, elapsed time, and percent of time for each function, as shown in the following figure.
0. The upper right corner of the 4 buttons to enlarge, reduce, real size, the appropriate size;
1. The arrow indicates the calling relation, and the caller points to the callee;
2. The upper-left corner of the rectangle shows the name of the module or function, and the upper-right corner shows the number of calls;
3. Rectangular display in the middle of running time and time percentage;
4. The color of the rectangle indicates the trend of elapsed time or percent of time: Red > Yellow green > Green, the fun3 rectangle is yellow-green, fun1 is green, and all fun3 run longer than FUN1.
5. It can be seen from the diagram that test.py directly invokes the FUN3, Fun1, fun2, and FUN5 functions; the FUN5 function directly invokes the FUN4 function; fun1, fun2, Fun3, Fun4, and fun5 all call print and sleep functions directly The total time of the whole test code is 6006ms, where the Fun3 run time is 1999ms, the percentage of time is 33.3%, that is, 1999ms/6006ms = 33.3%.