John W. Krahn wrote:
Mike Blezien wrote:

Hello,


Hello,


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 ];


John

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;'
        });

Benchmark: timing 10000000 iterations of loopy, plain...
loopy: 3 wallclock secs ( 4.69 usr + 0.00 sys = 4.69 CPU) @ 2132196.16/s (n=10000000) plain: 9 wallclock secs ( 8.94 usr + 0.00 sys = 8.94 CPU) @ 1118568.23/s (n=10000000)


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to