Most viewed recipes tagged "performance" Code Recipessocket.sendfile() (Python) 2014-06-13T13:41:25-07:00Giampaolo RodolĂ <p style="color: grey"> Python recipe 578889 by <a href="/recipes/users/4178764/">Giampaolo RodolĂ </a> (<a href="/recipes/tags/backport/">backport</a>, <a href="/recipes/tags/performance/">performance</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sendfile/">sendfile</a>). Revision 2. </p> <p>This is a backport of socket.sendfile() for Python 2.6 and 2.7. socket.sendfile() will be included in Python 3.5: <a href="" rel="nofollow"></a></p> Compare algorithms for heapq.smallest (Python) 2011-12-25T23:41:23-08:00Raymond Hettinger <p style="color: grey"> Python recipe 577573 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/benchmark/">benchmark</a>, <a href="/recipes/tags/heaps/">heaps</a>, <a href="/recipes/tags/performance/">performance</a>). Revision 3. </p> <p>General purpose technique for counting comparisons in various searching and sorting applications.</p> quick Python profiling with hotshot (Python) 2009-02-20T22:35:07-08:00Trent Mick <p style="color: grey"> Python recipe 576656 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/hotshot/">hotshot</a>, <a href="/recipes/tags/performance/">performance</a>, <a href="/recipes/tags/profile/">profile</a>). Revision 2. </p> <p>This is a quick snippet that I use occasionally to profile some pure-Python code, using <a href=""><code>hotshot</code></a>. Basically it is this:</p> <ol> <li>Put this <code>@hotshotit</code> decorator on the function you want to profile.</li> <li>Run your code through some representative paces. The result will be a <code>&lt;functionname&gt;.prof</code> in the current directory.</li> <li>Process the <code>.prof</code> file and print the top 20 hotspots with the given "" script.</li> </ol> <p>Props to <a href="">Todd</a> for slapping this code together.</p> <p>Hotshot <a href="">is a little out of favour</a> now, so I should -- or Todd :) -- should really come up with an equivalent that uses <code>cProfile</code>.</p> Fast and elegant switch/case-like dispatch (Python) 2011-09-02T01:49:40-07:00Jan Kaliszewski <p style="color: grey"> Python recipe 577864 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/case/">case</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/dispatcher/">dispatcher</a>, <a href="/recipes/tags/performance/">performance</a>, <a href="/recipes/tags/switch/">switch</a>). Revision 4. </p> <p>My approach to that common issue focuses on <strong>efficiency</strong> and <strong>elegant, declarative style</strong> of definition. That's why:</p> <ul> <li>The way switches work is based on unwrapped defaultdict/list lookup.</li> <li>The way you define switches is based on classes and easy-to-use decorators (note that you can use subclassing in a flexible way -- without any negative impact on efficiency!).</li> <li>Its use cases focus on a-lot-of-keys situations and it does not cover the <em>fall-through</em> feature (though you can reach its semantics if you really need that -- by calling class methods...).</li> </ul> IOPS calculator (Python) 2011-09-20T09:31:09-07:00Slava Yanson <p style="color: grey"> Python recipe 577873 by <a href="/recipes/users/4176967/">Slava Yanson</a> (<a href="/recipes/tags/iops/">iops</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/performance/">performance</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>Calculates IOPS for all disks in Linux/Unix system</p> A Simple Timing Function (Python) 2013-12-01T01:39:44-08:00Mike Sweeney <p style="color: grey"> Python recipe 578776 by <a href="/recipes/users/4177990/">Mike Sweeney</a> (<a href="/recipes/tags/performance/">performance</a>, <a href="/recipes/tags/timing/">timing</a>). Revision 2. </p> <p>This function prints out a message with the elapsed time from the previous call. It works with most Python 2.x platforms. The function uses a simple trick to store a persistent variable (clock) without using a global variable.</p> Using a getter for a one-time calculation of a JavaScript object attribute (JavaScript) 2010-07-16T18:10:51-07:00Trent Mick <p style="color: grey"> JavaScript recipe 577310 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/getter/">getter</a>, <a href="/recipes/tags/performance/">performance</a>). </p> <p>This is a technique for using a JavaScript getter to calculate the value of an attribute <strong>just the first time</strong>. Rather than caching the value in some private variable and returning it, you just delete the getter and put the calculated value in its place.</p> <p>Note: I'd read about this technique ages ago, but forgot the details and had to look-up "delete" for removing the getter. :)</p> Performance boost with metaclasses (Python) 2013-02-17T10:58:13-08:00Martin Schoepf <p style="color: grey"> Python recipe 578460 by <a href="/recipes/users/4185283/">Martin Schoepf</a> (<a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/performance/">performance</a>). </p> <p>Consider a highly dynamic system where a lot of objects are created and destroyed. If the objects are complex and have a lot of default values the following recipe may improve the performance. The more default values you have the more you will gain from it. </p> Generator with lookahead (Python) 2013-09-24T10:44:56-07:00Rutger Saalmink <p style="color: grey"> Python recipe 578671 by <a href="/recipes/users/4187940/">Rutger Saalmink</a> (<a href="/recipes/tags/background/">background</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/lookahead/">lookahead</a>, <a href="/recipes/tags/performance/">performance</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Python generators are a great way of reducing memory usage due to the lazy (on demand) generation of values. However, when the process-time of generating such a value is relatively high, we can improve performance even more by obtaining the next n values of the generator in a separate thread in the background. Hence, the BackgroundGenerator.</p>