[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] Sort quickie




On Jul 14, 2004, at 10:32 PM, Hugh Jarce wrote:
This sub can be written more efficiently as:
  sub by_date2 { substr($a,4) cmp substr($b,4) || $a cmp $b }
or:
  sub by_date3 { substr($a,4).$a cmp substr($b,4).$b }

Wow. Very slick.

Out of curiosity, I benchmarked 4 different ways to do it:
 
use strict;
use Benchmark;

-ponder- I need to study Benchmark. Looks cool. What's the point of @x? Can

   sub j9 { my @x = sort by_date @dates }

be written as

   sub j9 { sort by_date @dates }

?

sub j1 {
    my @x = map { substr($_,4) } sort map { substr($_,4).$_ } @dates
}

-laugh- Took me 3 minutes to figure out what that does. map tends to confuse my limited synapses.

Which surprised me. I expected j1 to be much faster.

Usually its faster for me to re-write a use of map than it is for me to figure out what I was doing when I wrote it. -grin- Typically I can't grok map at a glance, which slows me down. Can't argue w/ fast benchmarking though!

Thanks for the tips!

j