On Mar 2, 5:16 am, "rajendra" <rajendra.pra...@in.bosch.com> wrote:
> Hello All,
> In a script I use opendir() and readdir() functions to read the content of
> directory.
> I have observed one thing with this.The time taken to read the content get
> on better(lesser) with the successive execution of the script
> Is there a way I can reduce the time taken to read the content faster using
> the above two functions?.
readdir() in list context should be slightly faster than in scalar
context.
But TIMTOWTDI, you should also look at other functions/modules to read
the content of a directory.
If your overall algorithm calls opendir() multiple times to descend
into subdirectories, then using File::Find will not only be easier to
code, but might also be more efficient.
=======================
use strict;
use warnings;
my $dirname = '.';
print "\n\nMethod 1: readdir() in scalar context:\n"; {
opendir my $dh, $dirname or die "Error 0010: opendir $dirname,
reason: $!";
while (defined (my $item = readdir $dh)) {
print " Method 1 - found '$item'\n";
}
closedir $dh;
}
print "\n\nMethod 2: use glob() in scalar context:\n"; {
while (defined (my $item = glob $dirname.'/*')) {
print " Method 2 - found '$item'\n";
}
}
print "\n\nMethod 3: readdir() in list context:\n"; {
opendir my $dh, $dirname or die "Error 0020: opendir $dirname,
reason: $!";
for my $item (readdir $dh) {
print " Method 3 - found '$item'\n";
}
closedir $dh;
}
print "\n\nMethod 4: use glob() in list context:\n"; {
for my $item(glob $dirname.'/*') {
print " Method 4 - found '$item'\n";
}
}
print "\n\nMethod 5: use File::Find:\n"; {
use File::Find;
find(sub {
print " Method 5 - found '$_'\n";
}, $dirname);
}
=======================
--
Klaus
|