On Wed, 9 Jul 2008 13:23:13 +0100
Andy Armstrong <[EMAIL PROTECTED]> wrote:

> On 9 Jul 2008, at 09:09, David Kaufman wrote:
> >  # from the POD doco at
> >  #
> > http://search.cpan.org/~gbarr/Scalar-List-Utils-1.19/lib/List/Util.pm#DESCRIPTION
> >
> >  $foo = first { defined($_) } @list;  # first defined value in @list
> >
> >
> > Who needs to install a CPAN module to do that?  I personally would  
> > have
> > written it as:
> >
> >  $foo = (grep defined, @list)[0];  # first defined value in @list
> 
> 
> Bear in mind that first may be much more efficient if the list is  
> large and/or the test is expensive
  
  Agree on the efficiency.  I worked with a company on a large
  mod_perl application (300K+ lines of code) that used a particular
  function VERY often, that took an array and returned all the
  unique values.

  It did this with the common Perl Cookbook way of putting everything
  into a hash and returning the keys().  I switched that function
  to using List::MoreUtils::uniq() and they saw almost a 10% speed
  improvement to their entire application. Granted this was a special
  case because of how often that particular function was used (which
  was a bad design decision in the first place), but sometimes 
  using these types of modules is useful/necessary even if the
  normal "Perl core" way of doing it is relatively simple. 

  For example, if we use the technique above vs first() you get: 

  100 items in the list: 
                Rate  Grep first
      Grep   85470/s    --  -74%
      first 331126/s  287%    --
  
  5000 items in the list:  
                Rate  Grep first
      Grep    1743/s    --  -99%
      first 125000/s 7073%    --

  So even for relatively small lists it can be a huge performance
  win. 

 -------------------------------------------------------
   Frank Wiles, Revolution Systems, LLC. 
     Personal : [EMAIL PROTECTED]  http://www.wiles.org
     Work     : [EMAIL PROTECTED] http://www.revsys.com 

Reply via email to