opendir function.

R

rajendra

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?.

With Rgds,
Raj
 
A

Andrew DeFaria

rajendra said:
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?.
That is a function of your OS, in specific the filesystem, which caches
recently used files/dirs.
 
K

Klaus

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);
}
=======================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top