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?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|