Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Python includes a profiler called <a href="https://docs.python.org/3/library/profile.html#module-cProfile" rel="noreferrer">cProfile</a>. It not only gives the total running time, but also times each function separately, and tells you how many times each function was called, making it easy to determine where you should make optimizations.</p> <p>You can call it from within your code, or from the interpreter, like this:</p> <pre><code>import cProfile cProfile.run('foo()') </code></pre> <p>Even more usefully, you can invoke the cProfile when running a script:</p> <pre><code>python -m cProfile myscript.py </code></pre> <p>To make it even easier, I made a little batch file called 'profile.bat':</p> <pre><code>python -m cProfile %1 </code></pre> <p>So all I have to do is run:</p> <pre><code>profile euler048.py </code></pre> <p>And I get this:</p> <pre class="lang-none prettyprint-override"><code>1007 function calls in 0.061 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.061 0.061 &lt;string&gt;:1(&lt;module&gt;) 1000 0.051 0.000 0.051 0.000 euler048.py:2(&lt;lambda&gt;) 1 0.005 0.005 0.061 0.061 euler048.py:2(&lt;module&gt;) 1 0.000 0.000 0.061 0.061 {execfile} 1 0.002 0.002 0.053 0.053 {map} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler objects} 1 0.000 0.000 0.000 0.000 {range} 1 0.003 0.003 0.003 0.003 {sum} </code></pre> <p>EDIT: Updated link to a good video resource from PyCon 2013 titled <a href="https://web.archive.org/web/20170318204046/http://lanyrd.com/2013/pycon/scdywg/" rel="noreferrer"><strong><em>Python Profiling</em></strong></a><br> <a href="https://www.youtube.com/watch?v=QJwVYlDzAXs" rel="noreferrer">Also via YouTube</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload