Most viewed recipes tagged "performance"http://code.activestate.com/recipes/tags/performance/views/2014-06-13T13:41:25-07:00ActiveState Code Recipessocket.sendfile() (Python) 2014-06-13T13:41:25-07:00Giampaolo RodolĂ http://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/578889-socketsendfile/ <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="http://bugs.python.org/issue17552" rel="nofollow">http://bugs.python.org/issue17552</a></p> Compare algorithms for heapq.smallest (Python) 2011-12-25T23:41:23-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577573-compare-algorithms-for-heapqsmallest/ <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 Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/576656-quick-python-profiling-with-hotshot/ <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="http://docs.python.org/library/hotshot.html#module-hotshot"><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 "show_stats.py" script.</li> </ol> <p>Props to <a href="http://blogs.activestate.com/toddw/">Todd</a> for slapping this code together.</p> <p>Hotshot <a href="http://docs.python.org/library/profile.html">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 Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577864-fast-and-elegant-switchcase-like-dispatch/ <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 Yansonhttp://code.activestate.com/recipes/users/4176967/http://code.activestate.com/recipes/577873-iops-calculator/ <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 Sweeneyhttp://code.activestate.com/recipes/users/4177990/http://code.activestate.com/recipes/578776-a-simple-timing-function/ <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 Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577310-using-a-getter-for-a-one-time-calculation-of-a-jav/ <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 Schoepfhttp://code.activestate.com/recipes/users/4185283/http://code.activestate.com/recipes/578460-performance-boost-with-metaclasses/ <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 Saalminkhttp://code.activestate.com/recipes/users/4187940/http://code.activestate.com/recipes/578671-generator-with-lookahead/ <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>