官术网_书友最值得收藏!

Using the cProfile module

We will primarily be using the cProfile module to check our code. This module is a standard library function that is contained in every modern Python installation. We can run the profiler from the command line with -m cProfile, and specify that we want to organize the results by the cumulative time spent on each function with -s cumtime, and then redirect the output into a text file with the > operator.

This will work on both the Linux Bash or Windows PowerShell command line. 

Let's try this now:

We can now look at the contents of the text file with our favorite text editor. Let's keep in mind that the output of the program will be included at the beginning of the file:

Now, since we didn't remove the references to time in the original example, we see their output in the first two lines at the beginning. We can then see the total number of function calls made in this program, and the cumulative amount of time to run it.

Subsequently, we have a list of functions that are called in the program, ordered from the cumulatively most time-consuming functions to the least; the first line is the program itself, while the second line is, as expected, the simple_mandelbrot function from our program. (Notice that the time here aligns with what we measured with the time command). After this, we can see many libraries and system calls that relate to dumping the Mandelbrot graph to a file, all of which take comparatively less time. We use such output from cProfile to infer where our bottlenecks are within a given program.

主站蜘蛛池模板: 宜川县| 石楼县| 尤溪县| 大同县| 普洱| 兰溪市| 封丘县| 南丹县| 图木舒克市| 厦门市| 祁阳县| 峨边| 奈曼旗| 敦化市| 瑞昌市| 静乐县| 科尔| 承德市| 乌苏市| 顺昌县| 通河县| 甘肃省| 子洲县| 昌乐县| 朝阳县| 离岛区| 乐平市| 拜城县| 临湘市| 客服| 兰考县| 中卫市| 大冶市| 莎车县| 和龙市| 临汾市| 蚌埠市| 藁城市| 宁晋县| 沂源县| 前郭尔|