in reply to Re^4: Abusing Map (Corrected second code block)
in thread Abusing Map
Ignore this - it's the same error as the GP, sorry. I've now modified the script in the previous post to include this as the fourth comparison. It does look to be the most efficient as you suspected.
I've just tried that but it errors out with Modification of non-creatable array value attempted, subscript -2 at (eval 11) line 1.. Here's the code:
#!/usr/bin/env perl use strict; use warnings; use Benchmark 'cmpthese'; for my $arrsize (1e4, 1e6, 1e8) { print "Source array has $arrsize elements\n"; my @x; push @x, rand for 1..$arrsize; cmpthese (1e7, { 'BUK' => ' my @y; $y[ $_-1 ] = $x[ $_-1 ] - $x[ $_ ] for 1 .. +$#x; ', 'ike1' => ' my @y; $y[ $_ ] = $x[ $_ ] - $x[ $_+1 ] for 0 .. $ +#x-1; ', 'ike2' => ' my @y = @x; $y[ $_ ] -= $y[ $_+1 ] for 0 .. $#y-1; + pop @y; ', 'while' => ' my @y; my $i = $#x; $y[ $i ] = $x[ $i ] + $x[ --$ +i ] while $i; ', }); print '-' x 80 . "\n"; }
I'm not sure how it manages to get down to -2. Have I mis-typed your "while" version?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^6: Abusing Map (Corrected second code block)
by BrowserUk (Patriarch) on May 18, 2018 at 15:38 UTC |
In Section
Seekers of Perl Wisdom