When you add each file's lines to @content you are copying the entire array every time around the loop. This could quickly get expensive. It would probably be better to push onto the array, so instead of

@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


In reply to Re^2: Read from directory list... by johngg
in thread Read from directory list... by oblate kramrdc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.