Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have a look at <a href="http://docs.python.org/library/timeit.html" rel="noreferrer">timeit</a>, <a href="http://docs.python.org/library/profile.html" rel="noreferrer">the python profiler</a> and <a href="https://pycallgraph.readthedocs.org/en/master/" rel="noreferrer">pycallgraph</a>.</p> <h2>timeit</h2> <pre><code>def test(): """Stupid test function""" lst = [] for i in range(100): lst.append(i) if __name__ == '__main__': import timeit print(timeit.timeit("test()", setup="from __main__ import test")) </code></pre> <p>Essentially, you can pass it python code as a string parameter, and it will run in the specified amount of times and prints the execution time. The important bits from the docs:</p> <blockquote> <p><strong><code>timeit.timeit(stmt='pass', setup='pass', timer=&lt;default timer&gt;, number=1000000)</code></strong> </p> <blockquote> <p>Create a <code>Timer</code> instance with the given statement, <em>setup</em> code and <em>timer</em> function and run its <code>timeit</code> method with <em>number</em> executions.</p> </blockquote> </blockquote> <p>... and:</p> <blockquote> <p><strong><code>Timer.timeit(number=1000000)</code></strong></p> <blockquote> <p>Time <em>number</em> executions of the main statement. This executes the setup statement once, and then returns the time it takes to execute the main statement a number of times, measured in seconds as a float. The argument is the number of times through the loop, defaulting to one million. The main statement, the setup statement and the timer function to be used are passed to the constructor.</p> <p><strong>Note</strong></p> <blockquote> <p>By default, <code>timeit</code> temporarily turns off <code>garbage collection</code> during the timing. The advantage of this approach is that it makes independent timings more comparable. This disadvantage is that GC may be an important component of the performance of the function being measured. If so, GC can be re-enabled as the first statement in the <em>setup</em> string. For example:</p> <blockquote> <p><code>timeit.Timer('for i in xrange(10): oct(i)', 'gc.enable()').timeit()</code></p> </blockquote> </blockquote> </blockquote> </blockquote> <h2>Profiling</h2> <p>Profiling will give you a <em>much</em> more detailed idea about what's going on. Here's the "instant example" from <a href="http://docs.python.org/library/profile.html" rel="noreferrer">the official docs</a>:</p> <pre><code>import cProfile import re cProfile.run('re.compile("foo|bar")') </code></pre> <p>Which will give you:</p> <pre><code> 197 function calls (192 primitive calls) in 0.002 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.001 0.001 &lt;string&gt;:1(&lt;module&gt;) 1 0.000 0.000 0.001 0.001 re.py:212(compile) 1 0.000 0.000 0.001 0.001 re.py:268(_compile) 1 0.000 0.000 0.000 0.000 sre_compile.py:172(_compile_charset) 1 0.000 0.000 0.000 0.000 sre_compile.py:201(_optimize_charset) 4 0.000 0.000 0.000 0.000 sre_compile.py:25(_identityfunction) 3/1 0.000 0.000 0.000 0.000 sre_compile.py:33(_compile) </code></pre> <p>Both of these modules should give you an idea about where to look for bottlenecks.</p> <p>Also, to get to grips with the output of <code>profile</code>, have a look at <a href="https://stackoverflow.com/questions/1469679/understanding-python-profile-output">this post</a></p> <h2>pycallgraph</h2> <p><a href="http://docs.python.org/library/profile.html" rel="noreferrer">This module</a> uses graphviz to create callgraphs like the following:</p> <p><img src="https://i.stack.imgur.com/605RV.png" alt="callgraph example"></p> <p>You can easily see which paths used up the most time by colour. You can either create them using the pycallgraph API, or using a packaged script:</p> <pre><code>pycallgraph graphviz -- ./mypythonscript.py </code></pre> <p>The overhead is quite considerable though. So for already long-running processes, creating the graph can take some time.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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