Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > opendir function.

Reply
Thread Tools

opendir function.

 
 
rajendra
Guest
Posts: n/a
 
      03-02-2007
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


 
Reply With Quote
 
 
 
 
Andrew DeFaria
Guest
Posts: n/a
 
      03-02-2007
rajendra 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?.

That is a function of your OS, in specific the filesystem, which caches
recently used files/dirs.
--
Andrew DeFaria <http://defaria.com>
Why is it that the guy who comes up behind you while you're waiting for
an elevator presses the already lit button as though he has some magical
powers that you don't?

 
Reply With Quote
 
 
 
 
Klaus
Guest
Posts: n/a
 
      03-04-2007
On Mar 2, 5:16 am, "rajendra" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for a class similar to 'opendir' and 'readdir' Ramon Java 5 01-03-2005 01:59 PM
opendir doesn't work.. Markus Pitha C Programming 5 09-12-2004 02:40 PM
using "opendir" through a CGI script on Apache Alex Perl 2 02-10-2004 04:35 AM
opendir and if -d $files problem Bob Gervais Perl 1 11-12-2003 05:34 AM
opendir() Andrew Perl 2 09-03-2003 02:48 PM



Advertisments