in reply to What came (in) first, the push or the splice?
A couple of notes:
splice_it makes an entire copy of the array on the stack. It could have been hitting the limits of your system's memory in the test with the array of 100000 items.
while( $_ = shift( @cp ) ); is not equivalent to the others. It will end prematurely if a numeric zero (0), a string zero ('0'), a nul string ('') or an undefined value is encountered in @cp. Perhaps you should have used:
sub while_with_shift_push { my $ref; my @cp = @arr; push( @$ref, shift( @cp ) ) while @cp; return $ref; }
Probably irrelevant, but for ( 0..$#cp ) used more memory and was probably slower than for (my $i=@cp; $i--; ) in older versions of Perl.
Unless you plan on working with very long lists, I'd use splice_it. It's the easiest to read of the solutions with similar performance. (And this "unless" prooves your point.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: What came (in) first, the push or the splice?
by wazzuteke (Hermit) on May 08, 2006 at 16:43 UTC |