{"id":185,"date":"2023-09-08T11:08:59","date_gmt":"2023-09-08T14:08:59","guid":{"rendered":"https:\/\/vargolino.com\/wp\/?p=185"},"modified":"2023-09-08T11:10:06","modified_gmt":"2023-09-08T14:10:06","slug":"profiling-python-code","status":"publish","type":"post","link":"https:\/\/vargolino.com\/wp\/?p=185","title":{"rendered":"Quick Profiling Python Code"},"content":{"rendered":"\n<p>The cProfile is my go-to Python profiler as it is part of the default installation, no extra modules needed. When profilig with cProfile it will generate an output  with the call count and spent times for each called fuction. The main way I use it is specifying an output file for later inspection:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>python3 -m cProfile -o output_file myscript.py<\/code><\/code><\/pre>\n\n\n\n<p>This will generate a file named <code>output_file<\/code> to be opened and sorted and analysed later.  This file can be read with the pstats module (which is also a default module) by using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>python3 -m pstats output_file<\/code><\/code><\/pre>\n\n\n\n<p>You can call <code>sort<\/code> without arguments to see the options, but <code>sort time<\/code> and <code>sort cumulative<\/code> are the first ones I usually try and then <code>stats<\/code> will show the ordered data.<\/p>\n\n\n\n<p>There is also <a href=\"https:\/\/jiffyclub.github.io\/snakeviz\/\" data-type=\"link\" data-id=\"https:\/\/jiffyclub.github.io\/snakeviz\/\">snakeviz<\/a> which is a visualizer for the cProfile output file format. But this one you will need to install with: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>python3 -m pip install snakeviz<\/code><\/code><\/pre>\n\n\n\n<p class=\"has-large-font-size\">References:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.python.org\/3\/library\/profile.html#module-cProfile\">https:\/\/docs.python.org\/3\/library\/profile.html#module-cProfile<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/jiffyclub.github.io\/snakeviz\/\">https:\/\/jiffyclub.github.io\/snakeviz\/<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The cProfile is my go-to Python profiler as it is part of the default installation, no extra modules needed. When profilig with cProfile it will generate an output with the call count and spent times for each called fuction. The main way I use it is specifying an output file for later inspection: This will &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/vargolino.com\/wp\/?p=185\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Quick Profiling Python Code&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[7,25],"class_list":["post-185","post","type-post","status-publish","format-standard","hentry","category-solution","tag-howto","tag-python","entry"],"_links":{"self":[{"href":"https:\/\/vargolino.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vargolino.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vargolino.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vargolino.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vargolino.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=185"}],"version-history":[{"count":2,"href":"https:\/\/vargolino.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/185\/revisions"}],"predecessor-version":[{"id":187,"href":"https:\/\/vargolino.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/185\/revisions\/187"}],"wp:attachment":[{"href":"https:\/\/vargolino.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vargolino.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vargolino.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}