On Wed, Nov 10, 1999 at 05:56:13PM -0600, Brand Hilton wrote: > 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. I suppose that it would be a good optimization for Perl to automagically pre-allocate $#source elements for the target array. > Looks like I guessed right. I changed my subroutine from: > > sub mine { > my $half = $#array/2; > map (($_, $_+$half+1), 0..$half); > } Mmm, good choice. > 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; > } Ermmm ... doesn't that fill @retval with numbers rather than the values from the input array? That is, shouldn't the lines in the loop be: $retval[$i++] = @array[$_]; $retval[$i++] = @array[$_+$half+1]; Or am I on the wrong drugs? > 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 :-) Let's just say I'm sorry I don't need a columnizer anymore :) Cheers, Tom -- -- Tom Rathborne tomr@aceldama.com -- http://www.aceldama.com/~tomr/ -- "I seem to be having tremendous difficulty with my life-style." ==== Want to unsubscribe from Fun With Perl? Well, if you insist... ==== Send email to <fwp-request@technofile.org> with message _body_ ==== unsubscribe