in reply to aliasing
I think you got the answer to your question, but it might be worth pointing out that reverse already has smarts built in to reverse the elements of an array in-place if the source and destination are the same.
# memory use 2368k my @r = 1...1000000; # memory use 62804k @r = reverse @r; # memory use 62812k
As you can see, almost no extra memory is consumed by the process of the reversal. Contrast that with your aliased reverse in which an extra 16MB is consumed to reverse a million element array.
sub r{ @_[ 0 .. $#_ ] = reverse @_; } # memory use 2368k my @r = 1 .. 1000000; # memory use 62812k; r @r; # memory use 78664k;
Using reverse in the normal way is also markedly quicker.
#! perl -slw use strict; use Benchmark qw[ cmpthese ]; sub r{ @_[ 0 .. $#_ ] = reverse @_ } our @n = 1 .. 1000; cmpthese( -3, { normal_reverse => q[ my @r = reverse @n; ], aliased_reverse => q[ my @r = r( @n ); ], }); __END__ P:\test>320771 Rate aliased_reverse normal_reverse aliased_reverse 1360/s -- -78% normal_reverse 6110/s 349% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: aliasing
by dragonchild (Archbishop) on Jan 12, 2004 at 21:39 UTC | |
by BrowserUk (Patriarch) on Jan 12, 2004 at 22:14 UTC | |
|
Re: Re: aliasing
by ysth (Canon) on Jan 12, 2004 at 22:38 UTC | |
by BrowserUk (Patriarch) on Jan 12, 2004 at 22:55 UTC | |
by ysth (Canon) on Jan 12, 2004 at 23:26 UTC | |
by BrowserUk (Patriarch) on Jan 13, 2004 at 01:06 UTC | |
by ysth (Canon) on Jan 13, 2004 at 01:35 UTC | |
|