Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: •Re: Re: Create a list of directories without . and ..

by Juerd (Abbot)
on Jun 04, 2002 at 22:27 UTC ( [id://171654]=note: print w/replies, xml ) Need Help??


in reply to •Re: Re: Create a list of directories without . and ..
in thread Create a list of directories without . and ..

Sometimes a regex is overkill.

!/^\.\.?\z/.

Less readable, but faster and shorter.

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.

Replies are listed 'Best First'.
•Re: Re: •Re: Re: Create a list of directories without . and ..
by merlyn (Sage) on Jun 04, 2002 at 22:30 UTC

      Are you sure it's faster?

      I have benchmarked this before. It is faster. With short matches, regexes are often faster. /^#/ is faster than substr($_, 0, 1) eq '#'. (all assuming you'll have more non-matches than matches, which is true when checking when checking for . and ..)

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

        I'd be interested in seeing your results. I did some some benchmarking too, and found that the regex is slower.

        Here's the program (using my soon to be released 'Benchmark::Sized' module) and the results.

        #!/usr/bin/perl use strict; use warnings qw 'all'; use Benchmark::Sized; use vars qw /@array/; my $stats = sized_timethese run_for => 2, steps => 8, start_size => 1, end_size => 1000, seed => sub { @main::array = map {join "" => map {chr rand 128} 1 .. $_ [0]} + 1 .. 20 }, code => { regex => 'my @foo = grep {! /^\.\.?\Z/} @main::array', ne => 'my @foo = grep {$_ ne "." and $_ ne ".."} @main::ar +ray', } ; print "Runs/second for regex vs ne:\n"; print_timed $stats, type => 'sized'; __END__ Runs/second for regex vs ne: Size ne regex 1: 20744.28 12809.79 3: 22549.51 13048.06 7: 23110.45 12337.91 19: 21600.00 12560.28 52: 21005.50 12276.56 139: 19269.95 11891.63 373: 18345.79 11220.29 1000: 14422.54 9634.74

        Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://171654]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-25 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found