Professional Web Applications Themes

speed of grep{s///} vs ??? or am i asking the wrong question? - PERL Beginners

here's what works for me so far: #!/usr/bin/perl use strict; use warnings; sub get_subdirectories{ # retrieves list of directories from passed directory # returns directory list as an array my $directory = shift; open LS, "ls -l $directory|"; local $/ = undef; my dirs = grep {s/^d.*?\s*?(\w*)$/$1/} split ( /\n/, <LS>); } my results = get_subdirectories("/home/corenth"); print results; __END__ ------------------------------- now- just curious about speeding it up- pehaps a module would be fine, but i'd like to try this out explicitly. -- i think the s/// is what bothers me most. since i'm not all that comfortable with map() and ...

  1. #1

    Default speed of grep{s///} vs ??? or am i asking the wrong question?


    here's what works for me so far:

    #!/usr/bin/perl


    use strict;
    use warnings;

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

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


    }


    my results = get_subdirectories("/home/corenth");

    print results;

    __END__
    -------------------------------

    now- just curious about speeding it up- pehaps a module would be fine,
    but i'd like to try this out explicitly. -- i think the s/// is what
    bothers
    me most. since i'm not all that comfortable with map() and grep()
    especialy in block form (perldoc -f grep and perldoc -f map) i'm not
    sure if what i'm doing really takes advantage of the features that these
    functions have to offer.

    any advice?

    thanks :)

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

    William Guest

  2. #2

    Default RE: speed of grep{s///} vs ??? or am i asking the wrong question?

    > #!/usr/bin/perl 

    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;
    Luke Guest

  3. #3

    Default RE: speed of grep{s///} vs ??? or am i asking the wrong question?



     
    question? 

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

     
    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 "results\n\nresults2\n\n";



    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
    William Guest

  4. #4

    Default RE: speed of grep{s///} vs ??? or am i asking the wrong question?

    > >opendir LS, $directory or die "Can't opendir $directory: $!"; 
    >
    > thank you this is much more readable code too me- but i found out that
    > you need a fully qualified path...[/ref]

    Right, either that or chdir first before doing the readdir.
     

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

    No need for the backslash before the slash...
     

    Right, anytime you can avoid using system, qx, or `` you'll have a
    faster solution.

    Luke
    Luke Guest

Similar Threads

  1. frame speed question
    By Nate in forum Macromedia Flash Sitedesign
    Replies: 3
    Last Post: October 6th, 04:24 PM
  2. Question on grep search
    By Paul Harwood in forum PERL Beginners
    Replies: 3
    Last Post: November 16th, 04:01 PM
  3. speed question
    By todd in forum PHP Development
    Replies: 2
    Last Post: November 3rd, 11:49 PM
  4. Hi-speed video question
    By nina in forum Mac Applications & Software
    Replies: 9
    Last Post: September 8th, 08:25 AM
  5. Please HELP! ISO speed set to wrong speed.
    By Mick in forum Photography
    Replies: 4
    Last Post: July 27th, 02:44 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139