See http://sourceware.org/bugzilla/show_bug.cgi?id=1379 comment 4 In a polling loop, we generally don't need or want to print out an entire sorted array. sortn() just finds the top or bottom n elements. In a typical array of 1000 elements, sortn(10) runs in less than a tenth the time that sort() takes. Plus, if the array is not cleared, sortn() will have to do little work and the speed advantage will become even larger. Sorts are expensive and lock maps for a long time. So reducing the time the sorts take will improve accuracy by reducing the number of drops caused by attempted locks on maps failing.
This naturally ties in to iteration-limited array iteration. To express this in the language, one way is to extend the grammar in a way reminiscent of SQL: foreach ([i1,i2] in ray limit N) { } where N is a numeric expression evaluated at the beginning, and doesn't need to be a literal. The sorting +/- directives would stay as/where they are with current rules. How does the runtime sortn compare with sort for N approaching the map size?
Subject: Re: Use _stp_map_sortn(), via iteration-limited foreach On Tue, 2005-12-13 at 21:26 +0000, fche at redhat dot com wrote: > How does the runtime sortn compare with sort for N approaching the map size? For small map sizes, both sorts are close in speed. But sortn() approaches n**2 as N approaches the size of the map. I'd say just limit N to 30 for sortn(), otherwise go with sort().
Some time what I want to sort is not the value, but some key. How? can?
Created attachment 1399 [details] Patch that adds foreach "limit N" support Here's a patch that adds "foreach (key in array limit N)" support. The "limit" expression is evaluated once.
Nice work. A few parser & execution test cases, man page updates, searches for existing scripts that use the newly keywordized identifier "limit", and it's done. BTW there is no real need to post patches to bugzilla. Please feel free to commit straight to CVS or if you deem review required, then post to the mailing list.
Slightly modified patch checked in. Test cases added and man page updated.