Popular Python recipes tagged "meta:requires=timeit"http://code.activestate.com/recipes/langs/python/tags/meta:requires=timeit/2011-10-10T04:30:11-07:00ActiveState Code RecipesCompare speeds of different kinds of access to variables (Python)
2011-08-10T23:54:12-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577834-compare-speeds-of-different-kinds-of-access-to-var/
<p style="color: grey">
Python
recipe 577834
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/optimization/">optimization</a>).
Revision 5.
</p>
<p>Compare speeds of locals, nested scopes, global, builtins, instance variables, and class variables.</p>
Benchmark code with the with statement (Python)
2011-10-08T09:53:06-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577896-benchmark-code-with-the-with-statement/
<p style="color: grey">
Python
recipe 577896
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/benchmark/">benchmark</a>, <a href="/recipes/tags/speed/">speed</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/timer/">timer</a>).
</p>
<p>Inspired by <a href="http://preshing.com/20110924/timing-your-code-using-pythons-with-statement">this post</a> I wrote this context manager to benchmark code blocks or function calls.</p>
<p>Usage is incredibly simple:</p>
<pre class="prettyprint"><code>with Timer():
... # code to benchmark goes here
</code></pre>
<p>The time taken (in seconds) will be printed when the code block completes. To capture the time taken programmatically is almost as easy:</p>
<pre class="prettyprint"><code>t = Timer()
with t:
... # code to benchmark goes here
time_taken = t.interval
</code></pre>
<p>Due to the difficulties of timing small snippets of code <em>accurately</em>, you should only use this for timing code blocks or function calls which take a significant amount of time to process. For micro-benchmarks, you should use the <code>timeit</code> module.</p>
Benchmark code with the with statement (Python)
2011-10-10T04:30:11-07:00vleonhttp://code.activestate.com/recipes/users/4179537/http://code.activestate.com/recipes/577900-benchmark-code-with-the-with-statement/
<p style="color: grey">
Python
recipe 577900
by <a href="/recipes/users/4179537/">vleon</a>
(<a href="/recipes/tags/benchmark/">benchmark</a>, <a href="/recipes/tags/speed/">speed</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/timer/">timer</a>).
</p>
<p>Inspired by <a href="http://preshing.com/20110924/timing-your-code-using-pythons-with-statement">this post</a> I wrote this context manager to benchmark code blocks or function calls.</p>
<p>Usage is incredibly simple:</p>
<pre class="prettyprint"><code>with Timer():
... # code to benchmark goes here
</code></pre>
<p>The time taken (in seconds) will be printed when the code block completes. To capture the time taken programmatically is almost as easy:</p>
<pre class="prettyprint"><code>t = Timer()
with t:
... # code to benchmark goes here
time_taken = t.interval
</code></pre>
<p>Due to the difficulties of timing small snippets of code <em>accurately</em>, you should only use this for timing code blocks or function calls which take a significant amount of time to process. For micro-benchmarks, you should use the <code>timeit</code> module.</p>
Timing various python statements (Python)
2008-02-06T00:13:09-08:00Oliver Schoenbornhttp://code.activestate.com/recipes/users/1458241/http://code.activestate.com/recipes/544297-timing-various-python-statements/
<p style="color: grey">
Python
recipe 544297
by <a href="/recipes/users/1458241/">Oliver Schoenborn</a>
(<a href="/recipes/tags/programs/">programs</a>).
</p>
<p>The timeit module (in Python standard library) is handy to find out how fast a statement takes to execute, but not very convenient to compare speed of several equivalent statements: too much typing, need to create a Timer object for each statement, etc, tedious. The timings module provides the times() function to make it super easy to compare several statements in one call.</p>
Iterator merge 2 (Python)
2007-12-07T22:16:05-08:00Jonathan Croninhttp://code.activestate.com/recipes/users/4107146/http://code.activestate.com/recipes/535160-iterator-merge-2/
<p style="color: grey">
Python
recipe 535160
by <a href="/recipes/users/4107146/">Jonathan Cronin</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>Memory efficient multi-way iterator merge, without using heapq</p>
Extracting a low resolution grid from a high resolution grid (Python)
2005-05-23T15:28:15-07:00Gerry Wienerhttp://code.activestate.com/recipes/users/825001/http://code.activestate.com/recipes/414084-extracting-a-low-resolution-grid-from-a-high-resol/
<p style="color: grey">
Python
recipe 414084
by <a href="/recipes/users/825001/">Gerry Wiener</a>
(<a href="/recipes/tags/graphics/">graphics</a>).
Revision 3.
</p>
<p>This recipe illustrates how to extract a low resolution grid from a high resolution grid using the Numeric package. It presents an inefficient but straightforward solution and then a more efficient solution that employs the functionality in Numeric.</p>
Yet another timeit function (Python)
2005-04-01T07:57:55-08:00Thomas Hellerhttp://code.activestate.com/recipes/users/98141/http://code.activestate.com/recipes/408762-yet-another-timeit-function/
<p style="color: grey">
Python
recipe 408762
by <a href="/recipes/users/98141/">Thomas Heller</a>
(<a href="/recipes/tags/debugging/">debugging</a>).
</p>
<p>Here is a handy function to use the timeit module from a script, creating a nice overview of the runtimes of one or more code snippets.</p>
<p>All command line flags that the timeit module accepts can be used.</p>
<p>The output can easily be customized.</p>
Timeit module wrapper (Python)
2005-02-28T09:34:34-08:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/389767-timeit-module-wrapper/
<p style="color: grey">
Python
recipe 389767
by <a href="/recipes/users/760763/">Anand</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 4.
</p>
<p>A simple, easy to use wrapper function for doing quick tests
of your functions using the timeit module.</p>
Convenience 'timeit' function (Python)
2005-01-12T15:12:04-08:00m khttp://code.activestate.com/recipes/users/325981/http://code.activestate.com/recipes/361501-convenience-timeit-function/
<p style="color: grey">
Python
recipe 361501
by <a href="/recipes/users/325981/">m k</a>
(<a href="/recipes/tags/debugging/">debugging</a>).
Revision 2.
</p>
<p>This function makes it easier to use the timeit module from the interactive interpreter.</p>
<p>Just specify function with (optional) arguments to run, optional number of runs, and optional name of module (which if not specified defaults to the name of the function).</p>
<p>Example:</p>
<pre class="prettyprint"><code>>>> timefunc.timefunc('r()', 20)
20 loops, best of 3: 6.91e+004 usec per loop
>>> timefunc.timefunc('rx()', 20, 'r')
20 loops, best of 3: 2.23e+004 usec per loop
</code></pre>