How to use cProfile to analyze the performance of python for back testing
This article mainly explains "python how to use cProfile for performance analysis of back test", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "python how to use cProfile for back test performance analysis"!
Installation
There is no need to install, python usually comes with it.
Use
There are several ways to use it, and I output it directly. Is to put the test back code in a method, such as runBackTesting (). Then the main method runs according to the following code, which is sorted by cumulative time. Hint, it is best to comment out the Matplot image output, because the interaction time is also statistical.
If _ _ name__ = ='_ main__': cProfile.run ("runBackTesting ()", sort= "cumulative") performance analysis results
As shown in the following figure
The first line is the total number of function calls and the total elapsed time
The description of the following column
Ncalls: indicates the number of times a function is called
Tottime: indicates the total running time of the specified function, excluding the running time of the calling subfunction in the function
Percall: (the first percall) equals tottime/ncalls
Cumtime: indicates the time that the call to the function and all its subfunctions runs, that is, the time from the start of the call to the return of the function
Percall: (the second percall) is the average time for a function to run once, which is equal to cumtime/ncalls
Filename:lineno (function): the specific information of each function call; it is preceded by the file name, the number of lines, followed by the method name, and some methods, such as max,min, do not have a file name.
Thank you for your reading, the above is the content of "python how to use cProfile for performance analysis of back test". After the study of this article, I believe you have a deeper understanding of how python uses cProfile for performance analysis of back test, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!