{"id":4024,"date":"2020-08-23T05:34:35","date_gmt":"2020-08-23T05:34:35","guid":{"rendered":"https:\/\/machinelearningplus.com\/?p=4024"},"modified":"2022-03-08T16:37:13","modified_gmt":"2022-03-08T16:37:13","slug":"cprofile-how-to-profile-your-python-code","status":"publish","type":"post","link":"https:\/\/machinelearningplus.com\/python\/cprofile-how-to-profile-your-python-code\/","title":{"rendered":"cProfile  &#8211; How to profile your python code"},"content":{"rendered":"<p><em>Reducing code runtime is important for developers. Python Profilers, like cProfile helps to find which part of the program or code takes more time to run. This article will walk you through the process of using cProfile module for extracting profiling data, using the pstats module to report it and snakeviz for visualization.<\/em><\/p>\n<h2 id=\"contents\">By the end of this post, you will know:<\/h2>\n<ol>\n<li>Why do we need Python Profilers ?<\/li>\n<li>Introduction to cProfile<\/li>\n<li>How to use cProfile ?<\/li>\n<li>Profiling a function that calls other functions<\/li>\n<li>How to use Profile class of cProfile<\/li>\n<li>How to export cProfile data ?<\/li>\n<li>How to visualize cProfile reports?<\/li>\n<li>Profiling Linear Regression Model from scikit learn<\/li>\n<\/ol>\n<h2 id=\"whydoweneedpythonprofilers\">1. Why do we need Python Profilers ?<\/h2>\n<p>Today, there are so many of areas where you write code ranging from basic conditional logics to complex websites, apps, algorithms, etc. The main aspect while writing any code, especially when deploying, is that it should consume the lowest computational time and cost.<\/p>\n<p>This is especially important when you run code on cloud services like <a href=\"https:\/\/calculator.aws\/#\/\" target=\"_blank\" rel=\"noopener noreferrer\">AWS<\/a>, <a href=\"https:\/\/cloud.google.com\/products\/calculator\" target=\"_blank\" rel=\"noopener noreferrer\">Google Cloud<\/a> or <a href=\"https:\/\/azure.microsoft.com\/en-in\/pricing\/calculator\/\" target=\"_blank\" rel=\"noopener noreferrer\">Azure<\/a>, where there is a defined cost associated with the usage of computing resources. If you have two pieces of code that give the same result, the one that takes the least time and resource is usually chosen.<\/p>\n<p>Let&#8217;s say you have an algorithm that takes a lot of time to run. And you want to reduce the code run time. The first question that might crop up is:<\/p>\n<p><strong>Why does my code take so long to run?<\/strong><\/p>\n<p><strong>Python Profilers<\/strong> can answer that question. <strong>It tells you which part of the code took how long to run<\/strong>. This lets you focus on that particular part and achieve efficiency. I cover in detail how to use Python profiler, particularly &#8216;cProfile&#8217;, with various examples.<\/p>\n<h2 id=\"introductiontocprofile\">2. Introduction to cProfile<\/h2>\n<p><code>cProfile<\/code> is a built-in python module that can perform profiling. It is the most commonly used profiler currently.<\/p>\n<p>But, why <code>cProfile<\/code> is preferred?<\/p>\n<ol>\n<li>It gives you the <strong>total run time taken by the entire code<\/strong>.<\/li>\n<li>It also shows <strong>the time taken by each individual step<\/strong>. This allows you to compare and find which parts need optimization<\/li>\n<li>cProfile module also tells the <strong>number of times certain functions are being called<\/strong>.<\/li>\n<li>The data inferred can be <strong>exported easily<\/strong> using <code>pstats<\/code> module.<\/li>\n<li>The data can be <strong>visualized<\/strong> nicely using <code>snakeviz<\/code> module. Examples come later in this post.<\/li>\n<\/ol>\n<p>That&#8217;s a lot of useful information. Let&#8217;s look at the code example to use cProfile. Start by importing the package.<\/p>\n<pre><code class=\"python language-python\"># import module\nimport cProfile\n<\/code><\/pre>\n<h2 id=\"howtousecprofile\">3. How to use cProfile ?<\/h2>\n<p>cProfile provides a simple <strong><code>run()<\/code> function which is sufficient for most cases<\/strong>. The syntax is <code>cProfile.run(statement, filename=None, sort=-1)<\/code>.<\/p>\n<p>You can <strong>pass python code or a function name that you want to profile as a string<\/strong> to the <code>statement<\/code> argument.<\/p>\n<p>If you want to save the output in a file, it can be passed to the <code>filename<\/code> argument. The <code>sort<\/code> argument can be used to specify how the output has to be printed. By default, it is set to -1( no value).<\/p>\n<p>Let&#8217;s call <code>cProfile.run()<\/code> on a simple operation.<\/p>\n<pre><code class=\"python language-python\">import numpy as np\ncProfile.run(\"20+10\")\n<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>3 function calls in 0.000 seconds\n\n   Ordered by: standard name\n\n   ncalls  tottime  percall  cumtime  percall filename:lineno(function)\n        1    0.000    0.000    0.000    0.000 &lt;string&gt;:1(&lt;module&gt;)\n        1    0.000    0.000    0.000    0.000 {built-in method builtins.exec}\n        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}\n<\/code><\/pre>\n<p>Above you passed a simple addition code as a statement to the <code>run()<\/code> function of cProfile. Let&#8217;s understand the output.<\/p>\n<p>Line no.1: shows the number of function calls and the time it took to run.<\/p>\n<p>Line no.2: <code>Ordered by: standard name<\/code> means that the text string in the far right column was used to sort the output. This could be changed by the <code>sort<\/code> parameter.<\/p>\n<p>Line no. 3 onwards contain the functions and sub functions called internally. Let&#8217;s see what each column in the table means.<\/p>\n<ol>\n<li><code>ncalls<\/code> : Shows the number of calls made<\/li>\n<li><code>tottime<\/code>: Total time taken by the given function. Note that the time made in calls to sub-functions are excluded.<\/li>\n<li><code>percall<\/code>: Total time \/ No of calls. ( remainder is left out )<\/li>\n<li><code>cumtime<\/code>: Unlike <code>tottime<\/code>, this includes time spent in this and all subfunctions that the higher-level function calls. It is most useful and is accurate for recursive functions.<\/li>\n<li>The <code>percall<\/code> following <code>cumtime<\/code> is calculated as the quotient of <code>cumtime<\/code> divided by primitive calls. The primitive calls include all the calls that were not included through recursion.<\/li>\n<\/ol>\n<p>You could see that it isn&#8217;t very complex because the operation we did is simple.<\/p>\n<p>&nbsp;<\/p>\n<h2>4. Profiling a function that calls other functions<\/h2>\n<p>Now let&#8217;s try profiling on a code that calls other functions. In this case, you can pass the call to <code>main()<\/code> function as a string to <code>cProfile.run()<\/code> function.<\/p>\n<pre><code class=\"python language-python\"># Code containing multiple dunctions\ndef create_array():\n  arr=[]\n  for i in range(0,400000):\n    arr.append(i)\n\ndef print_statement():\n  print('Array created successfully')\n\n\ndef main():\n  create_array()\n  print_statement()\n\n\nif __name__ == '__main__':\n    cProfile.run('main()')\n<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>Array created successfully\n         400041 function calls in 0.091 seconds\n\n   Ordered by: standard name\n\n   ncalls  tottime  percall  cumtime  percall filename:lineno(function)\n        1    0.004    0.004    0.091    0.091 &lt;ipython-input-10-4dd6137cfe06&gt;:12(main)\n        1    0.059    0.059    0.087    0.087 &lt;ipython-input-10-4dd6137cfe06&gt;:3(create_array)\n        1    0.000    0.000    0.000    0.000 &lt;ipython-input-10-4dd6137cfe06&gt;:8(print_statement)\n        1    0.000    0.000    0.091    0.091 &lt;string&gt;:1(&lt;module&gt;)\n        3    0.000    0.000    0.000    0.000 iostream.py:195(schedule)\n        2    0.000    0.000    0.000    0.000 iostream.py:307(_is_master_process)\n        2    0.000    0.000    0.000    0.000 iostream.py:320(_schedule_flush)\n        2    0.000    0.000    0.000    0.000 iostream.py:382(write)\n        3    0.000    0.000    0.000    0.000 iostream.py:93(_event_pipe)\n        3    0.000    0.000    0.000    0.000 socket.py:357(send)\n        3    0.000    0.000    0.000    0.000 threading.py:1062(_wait_for_tstate_lock)\n        3    0.000    0.000    0.000    0.000 threading.py:1104(is_alive)\n        3    0.000    0.000    0.000    0.000 threading.py:506(is_set)\n        1    0.000    0.000    0.091    0.091 {built-in method builtins.exec}\n        2    0.000    0.000    0.000    0.000 {built-in method builtins.isinstance}\n        1    0.000    0.000    0.000    0.000 {built-in method builtins.print}\n        2    0.000    0.000    0.000    0.000 {built-in method posix.getpid}\n        3    0.000    0.000    0.000    0.000 {method 'acquire' of '_thread.lock' objects}\n        3    0.000    0.000    0.000    0.000 {method 'append' of 'collections.deque' objects}\n   400000    0.028    0.000    0.028    0.000 {method 'append' of 'list' objects}\n        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}\n<\/code><\/pre>\n<p>Observe the above output. Notice that when a particular function is called more than once, the <code>ncalls<\/code> value reflects that. You can also spot the difference between the <code>tottime<\/code> and <code>cumtime<\/code>. This output clearly tells you that <code>for i in range(0,400000)<\/code> is the part where majority of time is spent.<\/p>\n<h2 id=\"howtouseprofileclassofcprofile\">5. How to use Profile class of cProfile<\/h2>\n<p>What is the need for <code>Profile<\/code> class when you can simply do a <code>run()<\/code>?<\/p>\n<p>Even though the <code>run()<\/code> function of cProfile may be enough in some cases, there are certain other methods that are useful as well. The <code>Profile()<\/code> class of cProfile gives you more precise control. Let&#8217;s see a simple example.<\/p>\n<p>By default, cProfile sorts its output by \u201cstandard name\u201d. This means that it sorts by the filename(far right column). If you think of it, it&#8217;s actually not so useful, especially for complex functions. Also in case, the code contains a large number of steps, you cannot look through each line and find the time taken relatively.<\/p>\n<p><strong>How to use <code>Profile<\/code> to modify reports?<\/strong><\/p>\n<p>If your aim is to find the time-consuming parts, it would be helpful to sort the outputs as per <code>ncalls<\/code>. To do this,<\/p>\n<ol>\n<li>First, initialize an instance of Profile class.<\/li>\n<li>After that, call the <code>enable()<\/code> method of the profiler to start collecting profiling data.<\/li>\n<li>After that, call the function you want to profile.<\/li>\n<li>To stop collecting profiling data, call the <code>disable()<\/code> method.<\/li>\n<\/ol>\n<p><strong>How to report the data collected ?<\/strong><\/p>\n<p>The <code>pstats<\/code> module can be used to manipulate the results collected by the profiler object. First, create an instance of the stats class using <code>pstats.Stats<\/code>. Next, use the <code>Stats<\/code> class to create a statistics object from a profile object through <code>stats= pstats.Stats(profiler)<\/code>.Now, to sort the output by <code>ncalls<\/code>, use the <code>sort_stats()<\/code> method as shown below. Finally to print the output, call the function <code>print_statss()<\/code> of stats object.<\/p>\n<pre><code class=\"python language-python\"># How to use Profile class of cProfile\ndef create_array():\n  arr=[]\n  for i in range(0,400000):\n    arr.append(i)\n\ndef print_statement():\n  print('Array created successfully')\n\n\ndef main():\n  create_array()\n  print_statement()\n\nif __name__ == '__main__':\n    import cProfile, pstats\n    profiler = cProfile.Profile()\n    profiler.enable()\n    main()\n    profiler.disable()\n    stats = pstats.Stats(profiler).sort_stats('ncalls')\n    stats.print_stats()\n<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>Array created successfully\n         400039 function calls in 0.094 seconds\n\n   Ordered by: call count\n\n   ncalls  tottime  percall  cumtime  percall filename:lineno(function)\n   400000    0.034    0.000    0.034    0.000 {method 'append' of 'list' objects}\n        3    0.000    0.000    0.000    0.000 {method 'acquire' of '_thread.lock' objects}\n        3    0.000    0.000    0.000    0.000 {method 'append' of 'collections.deque' objects}\n        3    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/ipykernel\/iostream.py:93(_event_pipe)\n        3    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/ipykernel\/iostream.py:195(schedule)\n        3    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/zmq\/sugar\/socket.py:357(send)\n        3    0.000    0.000    0.000    0.000 \/usr\/lib\/python3.6\/threading.py:1104(is_alive)\n        3    0.000    0.000    0.000    0.000 \/usr\/lib\/python3.6\/threading.py:506(is_set)\n        3    0.000    0.000    0.000    0.000 \/usr\/lib\/python3.6\/threading.py:1062(_wait_for_tstate_lock)\n        2    0.000    0.000    0.000    0.000 {built-in method posix.getpid}\n        2    0.000    0.000    0.000    0.000 {built-in method builtins.isinstance}\n        2    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/ipykernel\/iostream.py:307(_is_master_process)\n        2    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/ipykernel\/iostream.py:320(_schedule_flush)\n        2    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/ipykernel\/iostream.py:382(write)\n        1    0.000    0.000    0.000    0.000 {built-in method builtins.print}\n        1    0.000    0.000    0.000    0.000 &lt;ipython-input-1-66b56f7cc511&gt;:6(print_statement)\n        1    0.004    0.004    0.094    0.094 &lt;ipython-input-1-66b56f7cc511&gt;:10(main)\n        1    0.055    0.055    0.090    0.090 &lt;ipython-input-1-66b56f7cc511&gt;:1(create_array)\n        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}\n<\/code><\/pre>\n<p>You can see that the above output is different from previous and is sorted by <code>ncalls<\/code>. You can sort the output in various other ways.<\/p>\n<p>Let&#8217;s say you want to sort the output by the cumulative time. You can do this by following the same code with a slight variation. Instead of <code>ncalls<\/code>, set <code>sort_stats<\/code> to use <code>cumtime<\/code>. The below code demonstrates it.<\/p>\n<pre><code class=\"python language-python\"># Sort output by Cumulative time\nif __name__ == '__main__':\n    import cProfile, pstats\n    profiler = cProfile.Profile()\n    profiler.enable()\n    main()\n    profiler.disable()\n    stats = pstats.Stats(profiler).sort_stats('cumtime')\n    stats.print_stats()\n<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>Array created successfully\n         400039 function calls in 0.088 seconds\n\n   Ordered by: cumulative time\n\n   ncalls  tottime  percall  cumtime  percall filename:lineno(function)\n        1    0.004    0.004    0.088    0.088 &lt;ipython-input-1-66b56f7cc511&gt;:10(main)\n        1    0.057    0.057    0.083    0.083 &lt;ipython-input-1-66b56f7cc511&gt;:1(create_array)\n   400000    0.026    0.000    0.026    0.000 {method 'append' of 'list' objects}\n        1    0.000    0.000    0.000    0.000 &lt;ipython-input-1-66b56f7cc511&gt;:6(print_statement)\n        1    0.000    0.000    0.000    0.000 {built-in method builtins.print}\n        2    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/ipykernel\/iostream.py:382(write)\n        3    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/ipykernel\/iostream.py:195(schedule)\n        3    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/zmq\/sugar\/socket.py:357(send)\n        3    0.000    0.000    0.000    0.000 \/usr\/lib\/python3.6\/threading.py:1104(is_alive)\n        2    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/ipykernel\/iostream.py:320(_schedule_flush)\n        3    0.000    0.000    0.000    0.000 \/usr\/lib\/python3.6\/threading.py:1062(_wait_for_tstate_lock)\n        2    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/ipykernel\/iostream.py:307(_is_master_process)\n        3    0.000    0.000    0.000    0.000 {method 'acquire' of '_thread.lock' objects}\n        3    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/ipykernel\/iostream.py:93(_event_pipe)\n        2    0.000    0.000    0.000    0.000 {built-in method posix.getpid}\n        3    0.000    0.000    0.000    0.000 \/usr\/lib\/python3.6\/threading.py:506(is_set)\n        3    0.000    0.000    0.000    0.000 {method 'append' of 'collections.deque' objects}\n        2    0.000    0.000    0.000    0.000 {built-in method builtins.isinstance}\n        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}\n<\/code><\/pre>\n<p>Likewise, you can sort by other parameters such as <code>percall<\/code>, <code>tottime<\/code> and so on. Refer this <a href=\"https:\/\/docs.python.org\/3.7\/library\/profile.html#pstats.Stats.sort_stats\">https:\/\/docs.python.org\/3.7\/library\/profile.html#pstats.Stats.sort_stats<\/a> to know all options.<\/p>\n<h2 id=\"howtoexportcprofiledata\">6. How to export cProfile data?<\/h2>\n<p>By default, the output of the profiler is simply printed out. But, you can use store the extracted data of profiling in a file as well. How to export the data\/report?<\/p>\n<p>The <code>pstats<\/code> module comes to use here.<\/p>\n<p>After creating a Stats instance, pass the profiler as input to it as shown below. After that, use <code>dump_stats()<\/code> method to store it to any file by providing the path.<\/p>\n<pre><code class=\"python language-python\"># Export profiler output to file\nstats = pstats.Stats(profiler)\nstats.dump_stats('\/content\/export-data')\n<\/code><\/pre>\n<p>Now, let&#8217;s consider a bit more lengthier example to organize the profiler output better. Let&#8217;s create a profile for the below code and print the report.<\/p>\n<pre><code class=\"python language-python\"># Using cProfile.Profile example\nimport random\n\ndef print_msg():\n    for i in range(10):\n        print(\"Program completed\")\n\ndef generate():\n    data = [random.randint(0, 99) for p in range(0, 1000)]\n    return data\n\ndef search_function(data):\n    for i in data:\n        if i in [100,200,300,400,500]:\n            print(\"success\")\n\ndef main():\n    data=generate()\n    search_function(data)\n    print_msg()\n\nif __name__ == '__main__':\n    import cProfile, pstats\n    profiler = cProfile.Profile()\n    profiler.enable()\n    main()\n    profiler.disable()\n    stats = pstats.Stats(profiler).sort_stats('tottime')\n    stats.print_stats()   \n<\/code><\/pre>\n<pre><code>Program completed\nProgram completed\nProgram completed\nProgram completed\nProgram completed\nProgram completed\nProgram completed\nProgram completed\nProgram completed\nProgram completed\n         5552 function calls in 0.003 seconds\n\n   Ordered by: internal time\n\n   ncalls  tottime  percall  cumtime  percall filename:lineno(function)\n     1000    0.001    0.000    0.002    0.000 \/usr\/lib\/python3.6\/random.py:173(randrange)\n     1000    0.001    0.000    0.001    0.000 \/usr\/lib\/python3.6\/random.py:223(_randbelow)\n     1000    0.001    0.000    0.002    0.000 \/usr\/lib\/python3.6\/random.py:217(randint)\n        1    0.000    0.000    0.003    0.003 &lt;ipython-input-30-2a521dc30378&gt;:7(&lt;listcomp&gt;)\n       21    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/zmq\/sugar\/socket.py:357(send)\n     1268    0.000    0.000    0.000    0.000 {method 'getrandbits' of '_random.Random' objects}\n        1    0.000    0.000    0.000    0.000 &lt;ipython-input-30-2a521dc30378&gt;:10(search_function)\n       20    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/ipykernel\/iostream.py:382(write)\n     1000    0.000    0.000    0.000    0.000 {method 'bit_length' of 'int' objects}\n       21    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/ipykernel\/iostream.py:195(schedule)\n        1    0.000    0.000    0.003    0.003 &lt;ipython-input-30-2a521dc30378&gt;:15(main)\n       21    0.000    0.000    0.000    0.000 \/usr\/lib\/python3.6\/threading.py:1104(is_alive)\n       10    0.000    0.000    0.001    0.000 {built-in method builtins.print}\n       20    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/ipykernel\/iostream.py:307(_is_master_process)\n       21    0.000    0.000    0.000    0.000 {method 'acquire' of '_thread.lock' objects}\n       21    0.000    0.000    0.000    0.000 \/usr\/lib\/python3.6\/threading.py:1062(_wait_for_tstate_lock)\n       20    0.000    0.000    0.000    0.000 {built-in method posix.getpid}\n        1    0.000    0.000    0.001    0.001 &lt;ipython-input-30-2a521dc30378&gt;:3(print_msg)\n       21    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/ipykernel\/iostream.py:93(_event_pipe)\n       20    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/ipykernel\/iostream.py:320(_schedule_flush)\n       21    0.000    0.000    0.000    0.000 {method 'append' of 'collections.deque' objects}\n       20    0.000    0.000    0.000    0.000 {built-in method builtins.isinstance}\n        1    0.000    0.000    0.003    0.003 &lt;ipython-input-30-2a521dc30378&gt;:6(generate)\n       21    0.000    0.000    0.000    0.000 \/usr\/lib\/python3.6\/threading.py:506(is_set)\n        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}\n<\/code><\/pre>\n<p>If you observe, you might find the output cluttered and difficult to read. How can we improve this?<\/p>\n<p>The <code>pstats<\/code> module provides the function <code>strip_dirs()<\/code> for this purpose. It removes all leading path information from file names.<\/p>\n<pre><code class=\"python language-python\"># Remove dir names\nstats.strip_dirs()\nstats.print_stats()\n<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>         5552 function calls in 0.003 seconds\n\n   Random listing order was used\n\n   ncalls  tottime  percall  cumtime  percall filename:lineno(function)\n     1000    0.000    0.000    0.000    0.000 {method 'bit_length' of 'int' objects}\n       20    0.000    0.000    0.000    0.000 {built-in method posix.getpid}\n       21    0.000    0.000    0.000    0.000 {method 'acquire' of '_thread.lock' objects}\n     1268    0.000    0.000    0.000    0.000 {method 'getrandbits' of '_random.Random' objects}\n       21    0.000    0.000    0.000    0.000 {method 'append' of 'collections.deque' objects}\n       20    0.000    0.000    0.000    0.000 {built-in method builtins.isinstance}\n       10    0.000    0.000    0.001    0.000 {built-in method builtins.print}\n        1    0.000    0.000    0.003    0.003 &lt;ipython-input-30-2a521dc30378&gt;:15(main)\n        1    0.000    0.000    0.003    0.003 &lt;ipython-input-30-2a521dc30378&gt;:6(generate)\n        1    0.000    0.000    0.000    0.000 &lt;ipython-input-30-2a521dc30378&gt;:10(search_function)\n        1    0.000    0.000    0.001    0.001 &lt;ipython-input-30-2a521dc30378&gt;:3(print_msg)\n        1    0.000    0.000    0.003    0.003 &lt;ipython-input-30-2a521dc30378&gt;:7(&lt;listcomp&gt;)\n       21    0.000    0.000    0.000    0.000 iostream.py:93(_event_pipe)\n       21    0.000    0.000    0.000    0.000 iostream.py:195(schedule)\n       20    0.000    0.000    0.000    0.000 iostream.py:307(_is_master_process)\n       20    0.000    0.000    0.000    0.000 iostream.py:320(_schedule_flush)\n       20    0.000    0.000    0.000    0.000 iostream.py:382(write)\n       21    0.000    0.000    0.000    0.000 socket.py:357(send)\n        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}\n     1000    0.001    0.000    0.002    0.000 random.py:173(randrange)\n     1000    0.001    0.000    0.002    0.000 random.py:217(randint)\n     1000    0.001    0.000    0.001    0.000 random.py:223(_randbelow)\n       21    0.000    0.000    0.000    0.000 threading.py:1104(is_alive)\n       21    0.000    0.000    0.000    0.000 threading.py:506(is_set)\n       21    0.000    0.000    0.000    0.000 threading.py:1062(_wait_for_tstate_lock)\n\n&lt;pstats.Stats at 0x7f58db5659e8&gt;\n<\/code><\/pre>\n<p>Observe the difference between the above and previous output. The above output is &#8220;random&#8221;. This is because, after a strip operation, the object has just been initialized and loaded.<\/p>\n<h2 id=\"howtovisualizecprofilereports\">7. How to visualize cProfile reports?<\/h2>\n<p>Even though we reduced some cluttering, there is still room to make it better. A good solution to get a clear picture of the profiling data is to visualize it.<\/p>\n<p>A best tool available at the moment for visualizing data obtained by <code>cProfile<\/code> module is <code>SnakeViz<\/code>.<\/p>\n<p>Let&#8217;s install it through the below command.<\/p>\n<pre><code class=\"python language-python\"># Installing the module\n!pip install snakeviz\n<\/code><\/pre>\n<pre><code>Collecting snakeviz\n[?25l  Downloading https:\/\/files.pythonhosted.org\/packages\/a2\/9a\/6c753d20af6f177d3cbdb05a4b2e4419db4ec021c50ba86aa0d13a784a5c\/snakeviz-2.1.0-py2.py3-none-any.whl (282kB)\n[K     |\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 286kB 2.8MB\/s \n[?25hRequirement already satisfied: tornado&gt;=2.0 in \/usr\/local\/lib\/python3.6\/dist-packages (from snakeviz) (5.1.1)\nInstalling collected packages: snakeviz\nSuccessfully installed snakeviz-2.1.0\n<\/code><\/pre>\n<p>For Ipython notebooks like google colab and Jupyter, you can load the SnakViz extension using <code>%load_ext snakeviz<\/code> command.<\/p>\n<p>After this, call the function or program&#8217;s profiling you want to visualize through the <code>%snakeviz &lt;filename&gt;<\/code>. The filename can be either the entire python script or call to a particular function.<\/p>\n<p>In the below code, I have written a main() function which calls several basic functions like creating an array and searching for specific elements. Now, to visualize the profiling data of the entire program I can use the command <code>%snakeviz main()<\/code> .<\/p>\n<pre><code class=\"python language-python\"># Code to test visualization\nimport random\n# Simple function to print messages \ndef print_msg():\n    for i in range(10):\n        print(\"Program completed\")\n\n# Generate random data\ndef generate():\n    data = [random.randint(0, 99) for p in range(0, 1000)]\n    return data\n\n# Function to search \ndef search_function(data):\n    for i in data:\n        if i in [100,200,300,400,500]:\n            print(\"success\")\n\ndef main():\n    data=generate()\n    search_function(data)\n    print_msg()\n\n\n%load_ext snakeviz\n%snakeviz main()\n<\/code><\/pre>\n<figure id=\"attachment_4041\" aria-describedby=\"caption-attachment-4041\" style=\"width: 640px\" class=\"wp-caption alignnone\"><img fetchpriority=\"high\" decoding=\"async\" class=\"wp-image-4041 size-large\" src=\"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-30-1024x691.png\" alt=\"cProfile Visualization - Snakeviz ( Icicle)\" width=\"640\" height=\"432\" srcset=\"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-30-1024x691.png 1024w, https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-30-300x202.png 300w, https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-30-768x518.png 768w, https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-30.png 1348w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption id=\"caption-attachment-4041\" class=\"wp-caption-text\">cProfile Visualization &#8211; Snakeviz ( Icicle)<\/figcaption><\/figure>\n<p>SnakeViz has two visualization styles, &#8216;icicle&#8217; and &#8216;sunburst&#8217;.<\/p>\n<p>By default, it&#8217;s icicle.\u00a0 icicle, the fraction of time taken by a code is represented by the width of the rectangle. Whereas in Sunburst, it is represented by the angular extent of an arc. You can switch between the two styles using the \u201cStyle\u201d dropdown.<\/p>\n<p>For the same code, let me show you the Sunburst style visualization too.<\/p>\n<figure id=\"attachment_4042\" aria-describedby=\"caption-attachment-4042\" style=\"width: 640px\" class=\"wp-caption alignnone\"><img decoding=\"async\" class=\"wp-image-4042 size-large\" src=\"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-35-1024x685.png\" alt=\"cProfile visualization \" width=\"640\" height=\"428\" srcset=\"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-35-1024x685.png 1024w, https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-35-300x201.png 300w, https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-35-768x514.png 768w, https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-35.png 1373w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption id=\"caption-attachment-4042\" class=\"wp-caption-text\">cProfile Visualization &#8211; SnakeViz ( Sunburst )<\/figcaption><\/figure>\n<h2 id=\"profilinglinearregressionmodelfromscikitlearn\">8. Profiling Linear Regression Model from scikit learn<\/h2>\n<p>Let&#8217;s look at a more complex example, where visualization would help a lot in real life.<\/p>\n<p>Regression problems are very commonly used for various predictive modeling problems. The below code is a standard Linear regression problem using the <code>sklearn<\/code> library. Let&#8217;s print the profiling reports for this code.<\/p>\n<pre><code class=\"python language-python\"># Function performing linear regression on diabetes dataset\ndef regression():\n    import numpy as np\n    from sklearn import datasets, linear_model\n    from sklearn.metrics import mean_squared_error, r2_score\n\n    # Load the diabetes dataset\n    diabetes_X, diabetes_y = datasets.load_diabetes(return_X_y=True)\n\n    # Use only one feature\n    diabetes_X = diabetes_X[:, np.newaxis, 2]\n\n    # Split the data into training\/testing sets\n    diabetes_X_train = diabetes_X[:-20]\n    diabetes_X_test = diabetes_X[-20:]\n\n    # Split the targets into training\/testing sets\n    diabetes_y_train = diabetes_y[:-20]\n    diabetes_y_test = diabetes_y[-20:]\n\n    # Create linear regression object\n    regr = linear_model.LinearRegression()\n\n    # Train the model using the training sets\n    regr.fit(diabetes_X_train, diabetes_y_train)\n\n    # Make predictions using the testing set\n    diabetes_y_pred = regr.predict(diabetes_X_test)\n\n\n# Initialize profile class and call regression() function\nprofiler = cProfile.Profile()\nprofiler.enable()\nregression()\nprofiler.disable()\nstats = pstats.Stats(profiler).sort_stats('tottime')\n\n# Print the stats report\nstats.print_stats()   \n<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>         364724 function calls (357697 primitive calls) in 0.847 seconds\n\n   Ordered by: internal time\n\n   ncalls  tottime  percall  cumtime  percall filename:lineno(function)\n       87    0.143    0.002    0.144    0.002 {built-in method _imp.create_dynamic}\n      379    0.110    0.000    0.110    0.000 {method 'read' of '_io.FileIO' objects}\n      739    0.088    0.000    0.089    0.000 \/usr\/lib\/python3.6\/inspect.py:2732(__init__)\n      379    0.055    0.000    0.055    0.000 {built-in method marshal.loads}\n    647\/2    0.048    0.000    0.848    0.424 {built-in method builtins.exec}\n     1589    0.037    0.000    0.037    0.000 {built-in method posix.stat}\n      379    0.026    0.000    0.136    0.000 &lt;frozen importlib._bootstrap_external&gt;:830(get_data)\n      347    0.023    0.000    0.062    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/scipy\/_lib\/doccer.py:12(docformat)\n  809\/806    0.019    0.000    0.031    0.000 {built-in method builtins.__build_class__}\n        1    0.013    0.013    0.013    0.013 \/usr\/local\/lib\/python3.6\/dist-packages\/scipy\/linalg\/lapack.py:784(_compute_lwork)\n    87\/79    0.012    0.000    0.030    0.000 {built-in method _imp.exec_dynamic}\n     8222    0.010    0.000    0.010    0.000 {method 'splitlines' of 'str' objects}\n      561    0.010    0.000    0.012    0.000 &lt;frozen importlib._bootstrap_external&gt;:1080(_path_importer_cache)\n      375    0.010    0.000    0.016    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/scipy\/_lib\/doccer.py:179(indentcount_lines)\n        1    0.008    0.008    0.021    0.021 \/usr\/local\/lib\/python3.6\/dist-packages\/scipy\/linalg\/basic.py:1047(lstsq)\n     .................\n     6172    0.004    0.000    0.004    0.000 {method 'replace' of 'str' objects}\n11729\/11727    0.004    0.000    0.005    0.000 {method 'join' of 'str' objects}\n      391    0.004    0.000    0.095    0.000 \/usr\/lib\/python3.6\/inspect.py:2102(_signature_from_function)\n     4862    0.004    0.000    0.004    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/numpy\/lib\/npyio.py:790(floatconv)\n      101    0.003    0.000    0.173    0.002 \/usr\/local\/lib\/python3.6\/dist-packages\/scipy\/stats\/_distn_infrastructure.py:1572(__init__)\n      379    0.003    0.000    0.208    0.001 &lt;frozen importlib._bootstrap_external&gt;:743(get_code)\n    487\/3    0.003    0.000    0.799    0.266 &lt;frozen importlib._bootstrap&gt;:966(_find_and_load)\n36731\/36652    0.003    0.000    0.003    0.000 {built-in method builtins.len}\n      554    0.003    0.000    0.003    0.000 {built-in method __new__ of type object at 0x9d12c0}\n    ..................\n      466    0.002    0.000    0.010    0.000 &lt;frozen importlib._bootstrap&gt;:504(_init_module_attrs)\n     1563    0.002    0.000    0.002    0.000 {method 'format' of 'str' objects}\n      355    0.002    0.000    0.108    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/scipy\/_lib\/_util.py:277(getargspec_no_self)\n     2757    0.002    0.000    0.003    0.000 &lt;frozen importlib._bootstrap_external&gt;:59(&lt;listcomp&gt;)\n 2575\/547    0.002    0.000    0.680    0.001 &lt;frozen importlib._bootstrap&gt;:997(_handle_fromlist)\n       36    0.002    0.000    0.002    0.000 {method 'read' of '_io.BufferedReader' objects}\n     1246    0.002    0.000    0.003    0.000 \/usr\/lib\/python3.6\/inspect.py:2452(__init__)\n      116    0.002    0.000    0.116    0.001 \/usr\/local\/lib\/python3.6\/dist-packages\/scipy\/stats\/_distn_infrastructure.py:622(_construct_argparser)\n       98    0.002    0.000    0.040    0.000 \/usr\/lib\/python3.6\/collections\/__init__.py:357(namedtuple)\n9839\/9838    0.002    0.000    0.002    0.000 {built-in method builtins.isinstance}\n   105\/31    0.002    0.000    0.005    0.000 \/usr\/lib\/python3.6\/sre_parse.py:470(_parse)\n      348    0.002    0.000    0.004    0.000 \/usr\/lib\/python3.6\/inspect.py:1787(_signature_bound_method)\n      580    0.002    0.000    0.004    0.000 \/usr\/lib\/python3.6\/functools.py:44(update_wrapper)\n      688    0.002    0.000    0.002    0.000 &lt;frozen importlib._bootstrap&gt;:103(release)\n        1    0.002    0.002    0.031    0.031 \/usr\/local\/lib\/python3.6\/dist-packages\/scipy\/__init__.py:58(&lt;module&gt;)\n .............\n        1    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/scipy\/sparse\/linalg\/_expm_multiply.py:315(LazyOperatorNormInfo)\n        1    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/scipy\/sparse\/linalg\/_norm.py:3(&lt;module&gt;)\n        1    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/scipy\/optimize\/_hessian_update_strategy.py:1(&lt;module&gt;)\n        1    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/scipy\/sparse\/linalg\/eigen\/arpack\/arpack.py:985(IterOpInv)\n        1    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/scipy\/sparse\/linalg\/interface.py:504(_CustomLinearOperator)\n        1    0.000    0.000    0.001    0.001 \/usr\/local\/lib\/python3.6\/dist-packages\/scipy\/optimize\/_trustregion_krylov.py:1(&lt;module&gt;)\n        1    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/scipy\/optimize\/_trustregion_exact.py:188(IterativeSubproblem)\n        1    0.000    0.000    0.000    0.000 &lt;string&gt;:5(MetricInfo)\n        1    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/scipy\/special\/sf_error.py:1(&lt;module&gt;)\n        1    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/scipy\/linalg\/decomp_cholesky.py:1(&lt;module&gt;)\npackages\/joblib\/externals\/cloudpickle\/cloudpickle.py:1139(_empty_cell_value)\n        1    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/joblib\/parallel.py:122(parallel_backend)\n        1    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/joblib\/parallel.py:322(BatchCompletionCallBack)\n        1    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/joblib\/_parallel_backends.py:578(SafeFunction)\n        1    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-packages\/joblib\/my_exceptions.py:30(WorkerInterrupt)\n        1    0.000    0.000    0.000    0.000 \/usr\/local\/lib\/python3.6\/dist-p\n.....(truncated)...\n\n\n\n\n&lt;pstats.Stats at 0x7f58cf8bb2e8&gt;\n<\/code><\/pre>\n<p>The above output has 207 entries!<\/p>\n<p>In this case, visualization is not just an improvement option, but a definite necessity.<\/p>\n<p>Let us apply SnakeViz visualization to the above code by calling the <code>regression()<\/code> function in the <code>%snakeviz regression()<\/code> command. This makes it very easier to interpret.<\/p>\n<pre><code class=\"python language-python\"># Load the extension for visualizer.\n\n%load_ext snakeviz\n%snakeviz regression()\n<\/code><\/pre>\n<figure id=\"attachment_4044\" aria-describedby=\"caption-attachment-4044\" style=\"width: 640px\" class=\"wp-caption alignnone\"><img decoding=\"async\" class=\"wp-image-4044 size-large\" src=\"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-29-1024x694.png\" alt=\"cProfile Visualization - Snakeviz ( SunBurst)\" width=\"640\" height=\"434\" srcset=\"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-29-1024x694.png 1024w, https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-29-300x203.png 300w, https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-29-768x520.png 768w, https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-29.png 1327w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption id=\"caption-attachment-4044\" class=\"wp-caption-text\">cProfile Visualization &#8211; Snakeviz ( Sunburst)<\/figcaption><\/figure>\n<figure id=\"attachment_4045\" aria-describedby=\"caption-attachment-4045\" style=\"width: 640px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4045 size-large\" src=\"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-32-1024x576.png\" alt=\"cProfile Visualization - Snakeviz ( Icicile)\" width=\"640\" height=\"360\" srcset=\"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-32-1024x576.png 1024w, https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-32-300x169.png 300w, https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-32-768x432.png 768w, https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-32-1536x864.png 1536w, https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Screenshot-32.png 1920w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption id=\"caption-attachment-4045\" class=\"wp-caption-text\">cProfile Visualization &#8211; Snakeviz ( Icicle)<\/figcaption><\/figure>\n<p>Note that you may not be able to get the visualizations properly in google colab. I recommend you to use Jupyter notebooks for convenience.<\/p>\n<p>You can check other options available <a href=\"https:\/\/jiffyclub.github.io\/snakeviz\/\">here<\/a> for snakeviz.<\/p>\n<h2>9. Conclusion<\/h2>\n<p>I hope you understood the importance of profiling and how to use the cProfile module to get the statistics. Apart from cProfile, there is the <code>Profile<\/code> module of python too. But, cProfile includes most of its features and hence is recommended. Stay tuned for similar posts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reducing code runtime is important for developers. Python Profilers, like cProfile helps to find which part of the program or code takes more time to run. This article will walk you through the process of using cProfile module for extracting profiling data, using the pstats module to report it and snakeviz for visualization. By the [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":4040,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[21],"tags":[1850,22,1865],"class_list":["post-4024","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-cprofile","tag-python","tag-python-profiler"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>cProfile - How to profile your python code | ML+<\/title>\n<meta name=\"description\" content=\"Python Profilers, like cProfile helps to find which part of the program or code takes more time to run. This article will walk you through the process of using cProfile module for extracting profiling data, using the pstats module to report it and snakeviz for visualization\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/localhost:8080\/python\/cprofile-how-to-profile-your-python-code\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"cProfile - How to profile your python code | ML+\" \/>\n<meta property=\"og:description\" content=\"Python Profilers, like cProfile helps to find which part of the program or code takes more time to run. This article will walk you through the process of using cProfile module for extracting profiling data, using the pstats module to report it and snakeviz for visualization\" \/>\n<meta property=\"og:url\" content=\"https:\/\/localhost:8080\/python\/cprofile-how-to-profile-your-python-code\/\" \/>\n<meta property=\"og:site_name\" content=\"machinelearningplus\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-23T05:34:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-08T16:37:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/localhost:8080\/wp-content\/uploads\/2020\/08\/Blue-Dynamic-Fitness-Youtube-Thumbnail-2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Shrivarsheni\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shrivarsheni\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"16 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/cprofile-how-to-profile-your-python-code\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/cprofile-how-to-profile-your-python-code\\\/\"},\"author\":{\"name\":\"Shrivarsheni\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/person\\\/31d782fda106181b2c88151a1d50a90d\"},\"headline\":\"cProfile &#8211; How to profile your python code\",\"datePublished\":\"2020-08-23T05:34:35+00:00\",\"dateModified\":\"2022-03-08T16:37:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/cprofile-how-to-profile-your-python-code\\\/\"},\"wordCount\":1698,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/cprofile-how-to-profile-your-python-code\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/Blue-Dynamic-Fitness-Youtube-Thumbnail-2.png\",\"keywords\":[\"cProfile\",\"Python\",\"Python Profiler\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/localhost:8080\\\/python\\\/cprofile-how-to-profile-your-python-code\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/cprofile-how-to-profile-your-python-code\\\/\",\"url\":\"https:\\\/\\\/localhost:8080\\\/python\\\/cprofile-how-to-profile-your-python-code\\\/\",\"name\":\"cProfile - How to profile your python code | ML+\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/cprofile-how-to-profile-your-python-code\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/cprofile-how-to-profile-your-python-code\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/Blue-Dynamic-Fitness-Youtube-Thumbnail-2.png\",\"datePublished\":\"2020-08-23T05:34:35+00:00\",\"dateModified\":\"2022-03-08T16:37:13+00:00\",\"description\":\"Python Profilers, like cProfile helps to find which part of the program or code takes more time to run. This article will walk you through the process of using cProfile module for extracting profiling data, using the pstats module to report it and snakeviz for visualization\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/localhost:8080\\\/python\\\/cprofile-how-to-profile-your-python-code\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/cprofile-how-to-profile-your-python-code\\\/#primaryimage\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/Blue-Dynamic-Fitness-Youtube-Thumbnail-2.png\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/Blue-Dynamic-Fitness-Youtube-Thumbnail-2.png\",\"width\":1280,\"height\":720,\"caption\":\"cProfile Python\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#website\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/\",\"name\":\"machinelearningplus\",\"description\":\"Learn Data Science (AI \\\/ ML) Online\",\"publisher\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/machinelearningplus.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\",\"name\":\"machinelearningplus\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/MachineLearningplus-logo.svg\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/MachineLearningplus-logo.svg\",\"width\":348,\"height\":36,\"caption\":\"machinelearningplus\"},\"image\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/person\\\/31d782fda106181b2c88151a1d50a90d\",\"name\":\"Shrivarsheni\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/0d1ccfbd64a15f0b94a69cc383b21358.jpg?ver=1776366584\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/0d1ccfbd64a15f0b94a69cc383b21358.jpg?ver=1776366584\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/0d1ccfbd64a15f0b94a69cc383b21358.jpg?ver=1776366584\",\"caption\":\"Shrivarsheni\"},\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/author\\\/shrivarsheni\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"cProfile - How to profile your python code | ML+","description":"Python Profilers, like cProfile helps to find which part of the program or code takes more time to run. This article will walk you through the process of using cProfile module for extracting profiling data, using the pstats module to report it and snakeviz for visualization","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/localhost:8080\/python\/cprofile-how-to-profile-your-python-code\/","og_locale":"en_US","og_type":"article","og_title":"cProfile - How to profile your python code | ML+","og_description":"Python Profilers, like cProfile helps to find which part of the program or code takes more time to run. This article will walk you through the process of using cProfile module for extracting profiling data, using the pstats module to report it and snakeviz for visualization","og_url":"https:\/\/localhost:8080\/python\/cprofile-how-to-profile-your-python-code\/","og_site_name":"machinelearningplus","article_published_time":"2020-08-23T05:34:35+00:00","article_modified_time":"2022-03-08T16:37:13+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/localhost:8080\/wp-content\/uploads\/2020\/08\/Blue-Dynamic-Fitness-Youtube-Thumbnail-2.png","type":"image\/png"}],"author":"Shrivarsheni","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Shrivarsheni","Est. reading time":"16 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/localhost:8080\/python\/cprofile-how-to-profile-your-python-code\/#article","isPartOf":{"@id":"https:\/\/localhost:8080\/python\/cprofile-how-to-profile-your-python-code\/"},"author":{"name":"Shrivarsheni","@id":"https:\/\/machinelearningplus.com\/#\/schema\/person\/31d782fda106181b2c88151a1d50a90d"},"headline":"cProfile &#8211; How to profile your python code","datePublished":"2020-08-23T05:34:35+00:00","dateModified":"2022-03-08T16:37:13+00:00","mainEntityOfPage":{"@id":"https:\/\/localhost:8080\/python\/cprofile-how-to-profile-your-python-code\/"},"wordCount":1698,"commentCount":0,"publisher":{"@id":"https:\/\/machinelearningplus.com\/#organization"},"image":{"@id":"https:\/\/localhost:8080\/python\/cprofile-how-to-profile-your-python-code\/#primaryimage"},"thumbnailUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Blue-Dynamic-Fitness-Youtube-Thumbnail-2.png","keywords":["cProfile","Python","Python Profiler"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/localhost:8080\/python\/cprofile-how-to-profile-your-python-code\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/localhost:8080\/python\/cprofile-how-to-profile-your-python-code\/","url":"https:\/\/localhost:8080\/python\/cprofile-how-to-profile-your-python-code\/","name":"cProfile - How to profile your python code | ML+","isPartOf":{"@id":"https:\/\/machinelearningplus.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/localhost:8080\/python\/cprofile-how-to-profile-your-python-code\/#primaryimage"},"image":{"@id":"https:\/\/localhost:8080\/python\/cprofile-how-to-profile-your-python-code\/#primaryimage"},"thumbnailUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Blue-Dynamic-Fitness-Youtube-Thumbnail-2.png","datePublished":"2020-08-23T05:34:35+00:00","dateModified":"2022-03-08T16:37:13+00:00","description":"Python Profilers, like cProfile helps to find which part of the program or code takes more time to run. This article will walk you through the process of using cProfile module for extracting profiling data, using the pstats module to report it and snakeviz for visualization","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/localhost:8080\/python\/cprofile-how-to-profile-your-python-code\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/localhost:8080\/python\/cprofile-how-to-profile-your-python-code\/#primaryimage","url":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Blue-Dynamic-Fitness-Youtube-Thumbnail-2.png","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2020\/08\/Blue-Dynamic-Fitness-Youtube-Thumbnail-2.png","width":1280,"height":720,"caption":"cProfile Python"},{"@type":"WebSite","@id":"https:\/\/machinelearningplus.com\/#website","url":"https:\/\/machinelearningplus.com\/","name":"machinelearningplus","description":"Learn Data Science (AI \/ ML) Online","publisher":{"@id":"https:\/\/machinelearningplus.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/machinelearningplus.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/machinelearningplus.com\/#organization","name":"machinelearningplus","url":"https:\/\/machinelearningplus.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/machinelearningplus.com\/#\/schema\/logo\/image\/","url":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/05\/MachineLearningplus-logo.svg","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/05\/MachineLearningplus-logo.svg","width":348,"height":36,"caption":"machinelearningplus"},"image":{"@id":"https:\/\/machinelearningplus.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/machinelearningplus.com\/#\/schema\/person\/31d782fda106181b2c88151a1d50a90d","name":"Shrivarsheni","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/0d1ccfbd64a15f0b94a69cc383b21358.jpg?ver=1776366584","url":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/0d1ccfbd64a15f0b94a69cc383b21358.jpg?ver=1776366584","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/0d1ccfbd64a15f0b94a69cc383b21358.jpg?ver=1776366584","caption":"Shrivarsheni"},"url":"https:\/\/machinelearningplus.com\/author\/shrivarsheni\/"}]}},"_links":{"self":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts\/4024","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/comments?post=4024"}],"version-history":[{"count":0,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts\/4024\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/media\/4040"}],"wp:attachment":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/media?parent=4024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/categories?post=4024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/tags?post=4024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}