J
James E Keenan
I need to build a hash whose elements represent characteristics of each of
9600 files in a single directory. I am trying to figure out why this hash
assignment is taking so long. Here is my code:
my %threads_chars = (
dir => "~/Threads",
pattern => "\.thr\.txt",
);
$threadsref = analyze_dir(\%threads_chars);
sub analyze_dir {
my $hashref = shift;
my $dir = ${$hashref}{'dir'};
my $pattern = ${$hashref}{'pattern'};
my (%filechars, $count);
chdir $dir or die "Couldn't change to $dir: $!";
opendir DIR, $dir or die "Couldn't open $dir: $!";
#1: get files in $dir which match pattern
my @files = grep { m|$pattern$|o } readdir DIR;
print '@files established for ', "$dir\n";
#2: build a hash where each element is keyed by the core of the
file name
# and the value is a ref to an array holding the file name, full
path, atime, mtime.
# Set a counter so I know how fast it's going
foreach (@files) {
$_ =~ m|^(.*)$pattern$|;
my $core = $1;
$filechars{$core} = [ $_, "$dir/$_", (stat($_))[8..9] ];
$count++;
if ($count % 100 == 0) { print 'count: ', "$count\n";}
}
closedir DIR or die "Couldn't close $dir: $!";
print "$dir HAS BEEN ANALYZED\n";
return \%filechars;
}
The counter prints to screen in 100-file increments. The first couple of
hundred files zip right by, but then the process slows to a crawl. This
occurred despite there being no other significant activity happening on this
disk. Why?
jimk
9600 files in a single directory. I am trying to figure out why this hash
assignment is taking so long. Here is my code:
my %threads_chars = (
dir => "~/Threads",
pattern => "\.thr\.txt",
);
$threadsref = analyze_dir(\%threads_chars);
sub analyze_dir {
my $hashref = shift;
my $dir = ${$hashref}{'dir'};
my $pattern = ${$hashref}{'pattern'};
my (%filechars, $count);
chdir $dir or die "Couldn't change to $dir: $!";
opendir DIR, $dir or die "Couldn't open $dir: $!";
#1: get files in $dir which match pattern
my @files = grep { m|$pattern$|o } readdir DIR;
print '@files established for ', "$dir\n";
#2: build a hash where each element is keyed by the core of the
file name
# and the value is a ref to an array holding the file name, full
path, atime, mtime.
# Set a counter so I know how fast it's going
foreach (@files) {
$_ =~ m|^(.*)$pattern$|;
my $core = $1;
$filechars{$core} = [ $_, "$dir/$_", (stat($_))[8..9] ];
$count++;
if ($count % 100 == 0) { print 'count: ', "$count\n";}
}
closedir DIR or die "Couldn't close $dir: $!";
print "$dir HAS BEEN ANALYZED\n";
return \%filechars;
}
The counter prints to screen in 100-file increments. The first couple of
hundred files zip right by, but then the process slows to a crawl. This
occurred despite there being no other significant activity happening on this
disk. Why?
jimk