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

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.

主站蜘蛛池模板: 项城市| 舟山市| 永泰县| 青海省| 望都县| 泾阳县| 马山县| 汤阴县| 汾阳市| 玉溪市| 宁安市| 贡觉县| 抚州市| 隆子县| 通榆县| 枞阳县| 建瓯市| 旺苍县| 乌兰县| 姜堰市| 遂宁市| 芮城县| 梁山县| 桂林市| 若羌县| 锦州市| 海盐县| 句容市| 余姚市| 启东市| 东兴市| 同仁县| 兴文县| 新龙县| 苏尼特左旗| 大港区| 奎屯市| 当雄县| 陆川县| 清河县| 油尖旺区|