in reply to Can't modify private array in concatenation?
@list = (@list, bsd_glob ("$DIR/*XYZ", GLOB_QUOTE )); ... push @list, bsd_glob ("$DIR/*XYZ", GLOB_QUOTE ));
(Update: Replaced the code sample I inadvertently copied from the OP with the one I meant to copy, from jettero).
But there's an interesting difference which becomes very clear for very large lists, which is that using push is more efficient.
For example, running the following takes about 1 second for the subroutine list_push to run, but 3 to 5 seconds for the subroutine list_combine:
use strict; use warnings; my $size = 1_000_000; list_push(); #list_combine(); sub list_combine { print "Creating list1 ...\n"; my @list1 = ( "a" ) x $size; print "Creating list2 ...\n"; my @list2 = ( "b" ) x $size; print "Concatenating lists...\n"; my $start = time; @list1 = (@list1, @list2); my $elapsed = time - $start; my $size = @list1; print "List size = $size, elapsed = $elapsed second(s)\n"; } sub list_push { print "Creating list1 ...\n"; my @list1 = ( "a" ) x $size; print "Creating list2 ...\n"; my @list2 = ( "b" ) x $size; print "Pushing lists...\n"; my $start = time; push @list1, @list2; printf "Elapsed = %d\n", time - $start; my $elapsed = time - $start; my $size = @list1; print "List size = $size, elapsed = $elapsed second(s)\n"; }
It gets much worse for $size = 2_000_000, where list_combine causes my computer thrash as it runs out of available memory.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Can't modify private array in concatenation?
by chromatic (Archbishop) on Dec 22, 2006 at 23:43 UTC | |
by jettero (Monsignor) on Dec 23, 2006 at 01:15 UTC | |
by sgt (Deacon) on Dec 23, 2006 at 15:16 UTC |