develooper Front page | perl.perl5.porters | Postings from July 2004

Re: optimizing Short circuiting sort method based on [M]

Thread Previous | Thread Next
From:
david nicol
Date:
July 8, 2004 18:45
Subject:
Re: optimizing Short circuiting sort method based on [M]
Message ID:
1089337519.1041.285.camel@plaza.davidnicol.com
On Wed, 2004-06-30 at 16:08, Glenn Linderman wrote:
> I can also imagine code like
> 
>    while ( sort @array )
>    {
>       ...
>       last  if ... ;
>       ...
>    }
>
> for large @array, I speculate that it might often be more efficient to 
> use an incremental sort than to do a full sort, and only use a fraction 
> of the results.  Of course, it depends on the likelihood of using a 
> small fraction, so probably in cases like this a user hint should 
> determine whether to use a full sort, or an incremental sort.

    use Lazy::Sort;
    {
      tie my @Lazy => Lazy::Sort => sub($$){$_[0] cmp $_[1]} => @Array;
      local *_;
      while(defined ($_ = shift @Lazy)){
         ...
         last if ...
      }
      ...
    }

I'm about to post Lazy::Sort to p5p, enough of it to do that at least,
encourage me to CPANify it and I expect I will.  A better interface
would be good though.


-- 
david nicol
"cat and buttered toast" 


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About