opendir function.

Discussion in 'Perl Misc' started by rajendra, Mar 2, 2007.

  1. rajendra

    rajendra Guest

    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
    rajendra, Mar 2, 2007
    #1
    1. Advertising

  2. 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?
    Andrew DeFaria, Mar 2, 2007
    #2
    1. Advertising

  3. rajendra

    Klaus Guest

    On Mar 2, 5:16 am, "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?.


    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
    Klaus, Mar 4, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew

    opendir()

    Andrew, Sep 3, 2003, in forum: Perl
    Replies:
    2
    Views:
    9,460
    John Bokma
    Sep 3, 2003
  2. Bob Gervais

    opendir and if -d $files problem

    Bob Gervais, Nov 7, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,637
    Steve Grazzini
    Nov 12, 2003
  3. Alex
    Replies:
    2
    Views:
    692
  4. Ramon
    Replies:
    5
    Views:
    10,783
    John C. Bollinger
    Jan 3, 2005
  5. Markus Pitha

    opendir doesn't work..

    Markus Pitha, Sep 12, 2004, in forum: C Programming
    Replies:
    5
    Views:
    465
    Markus Pitha
    Sep 12, 2004
Loading...

Share This Page