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

Re: [FWP] Interleaving arrays



On Wed, Nov 10, 1999 at 03:03:06PM -0600, I wrote:
> 
> Oddly, benchmarking indicates that Tom's way is about twice as fast as
> mine, which was:
> 
>   my $half = $#cats/2;
>   @cats = @cats[map (($_, $_+$half+1), 0..$half)];
> 
> Knowing next to nothing about Perl internals, I'm guessing that it's
> something to do with growing the list at each iteration of the map.

Looks like I guessed right.  I changed my subroutine from:

  sub mine {
    my $half = $#array/2;
    map (($_, $_+$half+1), 0..$half);
  }

to the substantially more verbose:

  sub mine2 {
    my $half = $#array/2;
    my @retval;
    $#retval = $#array;
    my $i = 0;
    foreach (0..$half) {
      $retval[$i++] = $_;
      $retval[$i++] = $_+$half+1;
    }
    @retval;
  }

The speedup was quite noteworthy:

Benchmark: timing 100 iterations of Mine, Mine improved, Tom's, Tom's improved...
      Mine: 42 wallclock secs (41.01 usr +  0.05 sys = 41.06 CPU)
Mine improved: 10 wallclock secs ( 9.91 usr +  0.01 sys =  9.92 CPU)
     Tom's: 23 wallclock secs (22.25 usr +  0.01 sys = 22.26 CPU)
Tom's improved: 18 wallclock secs (16.98 usr +  0.00 sys = 16.98 CPU)

I hope someone besides me is having fun with this :-)

        Brand

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe