in reply to Re: Read from directory list...
in thread Read from directory list...
@content= (@content, <FH>);
you could do
push @content, <FH>;
I ran a benchmark of these two methods on a sample of 65 scripts totalling 2082 lines, "Copy" being your original and "Push" my suggested alternative. Here's the benchmark code
use strict; use warnings; use Benchmark q{cmpthese}; my @files = grep { -f } <spw59*>; my $rcCopy = sub { my @content = (); foreach my $file ( @files ) { open my $fh, q{<}, $file or die qq{open: $file: $!\n}; @content = (@content, <$fh>); close $fh or die qq{close: $file: $!\n}; } return \@content; }; my $rcPush = sub { my @content = (); foreach my $file ( @files ) { open my $fh, q{<}, $file or die qq{open: $file: $!\n}; push @content, <$fh>; close $fh or die qq{close: $file: $!\n}; } return \@content; }; cmpthese (-60, { Copy => $rcCopy, Push => $rcPush, });
and here's the result
Rate Copy Push Copy 2.32/s -- -94% Push 41.9/s 1710% --
As you can see, copying the array each time takes a lot of resources.
Cheers,
JohnGG
|
|---|