Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

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


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

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.

Replies are listed 'Best First'.
Re: Create a list of directories without . and ..
by Abigail-II (Bishop) on Jun 05, 2002 at 10:57 UTC

    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

      Unfortunately, I don't have your shiny Benchmark::Sized (I'm looking forward to its release), but there are my results using Benchmark::cmpthese:

      2;130 juerd@ouranos:~/modje/Blah/Acme/Tie/Handle/Blah/Acme-Tie-Handle- +Blah--0.10$ perl -MBenchmark=cmpthese -e'$_="fooooo"; cmpthese(-1, {r +e => sub {$a = !/^\.\.?\z/}, ne => sub { $a = ($_ ne "." and $_ ne ". +."); }})' Benchmark: running ne, re, each for at least 1 CPU seconds... ne: 1 wallclock secs ( 1.02 usr + -0.01 sys = 1.01 CPU) @ 12 +18808.91/s (n=1230997) re: 1 wallclock secs ( 1.09 usr + 0.00 sys = 1.09 CPU) @ 18 +03743.12/s (n=1966080) Rate ne re ne 1218809/s -- -32% re 1803743/s 48% --
      Maybe this is because you used \Z, which is equivalent to $ if /s is not used. \z should be used (explained elsewhere in this thread).
      2;0 juerd@ouranos:~/modje/Blah/Acme/Tie/Handle/Blah/Acme-Tie-Handle-Bl +ah--0.10$ perl -MBenchmark=cmpthese -e'$_="foo"; cmpthese(-0.3, { z = +> sub { /o\z/ }, Z => sub { /o\Z/ } });' Benchmark: running Z, z, each for at least 0.3 CPU seconds... Z: 2 wallclock secs ( 0.43 usr + 0.02 sys = 0.45 CPU) @ 36 +70015.56/s (n=1651507) z: 1 wallclock secs ( 0.44 usr + -0.01 sys = 0.43 CPU) @ 44 +19869.77/s (n=1900544) Rate Z z Z 3670016/s -- -17% z 4419870/s 20% --

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-18 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found