Tom Allison wrote: > John W. Krahn wrote: >> Mike Blezien wrote: >> >>> what is the most effecient way to combine multiple array refs into one >>> array ref, IE: >>> >>> my $arrayrefA = ['1',2','3']; >>> my $arrayrefB = ['4','5','6']; >>> my $arrayrefC = ['7','8','9']; >>> >>> my $allarrayref = (Combine $arrayrefA $arrayrefB $arrayrefC) >> >> So you just want to combine the contents of the anonymous arrays? >> >> my $allarrayref = [ @$arrayrefA, @$arrayrefB, @$arrayrefC ]; > > You might want to Benchmark this. > I know from the perl cookbook that joining two hashes can be done this > way but it's also mentioned that it is very memory intensive. I don't > know if the same applies here. > > My results show that this is not the best way to procede if you are > interested in performance. > > use strict; > use warnings; > > use Benchmark; > use Time::HiRes; > > my $A = [1,2,3,4,5,6,7,8,9]; > my $B = [11,12,13,14,15,16,17,18,19]; > > timethese(10000000, > { > 'plain' => 'my $array = [$A, $B];', > 'loopy' => 'push @$A, $_ foreach @$B;' > });
Those aren't equivalent. 'plain' is only assigning two scalars while 'loopy' is pushing nine scalars. 'plain' => 'my $array = [EMAIL PROTECTED], @$B];', 'loopy' => 'push @$array, $_ foreach @$A, @$B;' But you don't need a foreach loop with push(): 'plain' => 'my $array = [EMAIL PROTECTED], @$B];', 'loopy' => 'push @$array, @$A, @$B;' $ perl -e' use Benchmark q/cmpthese/; my $A = [ 1 .. 9 ]; my $B = [ 11 .. 19 ]; cmpthese ( 10_000_000, { scalar_assign => q/my $array = [ @$A, @$B ]/, array_assign => q/my $array; @$array = ( @$A, @$B )/, map_assign => q/my $array; @$array = map @$_, $A, $B/, push => q/my $array; push @$array, @$A, @$B/, push_loop1 => q/my $array; push @$array, $_ for @$A, @$B/, push_loop2 => q/my $array; push @$array, @$_ for $A, $B/, } ); ' Rate push_loop2 push_loop1 map_assign scalar_assign array_assign push push_loop2 550358/s -- -32% -49% -60% -69% -69% push_loop1 812348/s 48% -- -25% -40% -54% -55% map_assign 1084599/s 97% 34% -- -20% -38% -40% scalar_assign 1360544/s 147% 67% 25% -- -23% -24% array_assign 1757469/s 219% 116% 62% 29% -- -2% push 1795332/s 226% 121% 66% 32% 2% -- John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>