>-----Original Message-----
>From: Bakken, Luke [mailto:[EMAIL PROTECTED]
>Sent: Monday, October 04, 2004 5:06 PM
>To: West, William M; [EMAIL PROTECTED]
>Subject: RE: speed of grep{s///} vs ??? or am i asking the wrong
question?
>
>Try this, you don't need to shell out. It returns an array of
>subdirectories.
>
>opendir LS, $directory or die "Can't opendir $directory: $!";
>my @dirs = grep { -d $_ } readdir LS;
>closedir LS;
>return @dirs;

thank you this is much more readable code too me- but i found out that
you need a fully qualified path...


>my @dirs = grep { -d $_ } readdir LS;
should be:

my @dirs = grep { -d "$directory\/$_" } readdir LS;

or something of the sort...  took me a long long time before i finally
looked it up here: http://www.perlmonks.org/index.pl?node=readdir


hmmm..  with 10000 iterations here's a benchmark::


                       Rate sub_with_substitution      sub_with_readdir
sub_with_substitution 410/s                    --                  -35%
sub_with_readdir      633/s                   54%                    --


using readdir seems to take a bit less time... while i'm typing this,
i'm testing with 100000 iterations.

Here is the benchmark script::



#!/usr/bin/perl


use strict;
use diagnostics;
use Benchmark qw/cmpthese/;


sub get_subdirectories{
        # retrieves list of directories from passed directory
        #returns list as an array

        my $directory = "/home/corenth";
        open LS, "ls -l $directory|";
        local $/ = undef;
        #my @dirs =
        grep {s/^d.*?\s*?(\w*)$/$1/} split ( /\n/, <LS>);


}


sub get_sb2 {
 # retrieves list of directories from passed directory
        #returns list as an array

        my $directory = "/home/corenth";
        opendir LS2, $directory or die "no opendir $directory: $!";
        #my @dirs =
       my @array =grep {-d "$directory\/$_"} readdir LS2;
        closedir LS2;
        return @array;

}

my @results = get_subdirectories("/home/corenth");
my @results2 = get_sb2();
print "@[EMAIL PROTECTED]";



cmpthese(100000,{
        sub_with_substitution =>\&get_subdirectories,
        sub_with_readdir => \&get_sb2,
});


__END__



looks like readdir()  is the all around winner :)



willy
http://www.hackswell.com/corenth

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to