A couple of things to try. First, it's not clear from your snippet whether you're concatenating the directory to each file or not. Given a large directory, you're better off doing a chdir there, then opening and reading from ".", because each stat doesn't have to traverse the path. (It's probably cached, depending on your kernel, but this way it won't even matter.) As I said, maybe you're already doing this, but it's not clear from the code.

Another thing is to separate reading the directory from stat'ing it. That is, slurp the directory contents in one go, then traverse through the array. This will eat up a lot more memory, but it may be a worthwhile tradeoff (you'll have to make that call). Combining those, your loop would look like something like this:

my $dir = '/some/where/'; chdir $dir or die "Can't chdir $dir\n"; opendir D, '.' or die "Can't opendir '.'\n"; my @files = readdir(D); closedir D; my %filesize; foreach my $f (@files) { $filesize{$f} = -s $f; }

None of these are guaranteed to make it faster; that actually depends on your underlying operating system more. But they may be worth trying.

HTH


In reply to Re: I'm falling asleep here by VSarkiss
in thread -s takes too long on 15,000 files by ishk0

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.