Re: shootout!

[email protected] (Karsten Sperling)
Newsgroups perl.golf
Message-ID <[email protected]>
On Wednesday, September 10, 2003, Phil Carmody wrote:
> Do any of you gurus have anything to say about the regexp shootout at:
>   http://www.bagley.org/~doug/shootout/bench/regexmatch/
> including the perl contender:
>   http://www.bagley.org/~doug/shootout/bench/regexmatch/regexmatch.perl
> ?


The following code is about 2.8 times faster for big $NUM on
my machine. Avoiding the alternation by using a look-behind
and the shorter loop without inner blocks each contribute about
the same speedup.

- Karsten


use strict;

my $re = qr{
    (?<![\d(])                  # not preceded by digit or (
    ( \( )?                     # match 1: possible initial left paren
    (\d\d\d)                    # match 2: area code is 3 digits
    (?(1) \) )                  # if match1 then match right paren
    [ ]                         # area code followed by one space
    (\d\d\d)                    # match 3: prefix of 3 digits
    [ -]                        # separator is either space or dash
    (\d\d\d\d)                  # match 4: last 4 digits
    \D                          # must be followed by a non-digit
}x;

my $NUM = 0 + shift;
$NUM = 1 if $NUM < 1;

my @phones = <STDIN>;
my $count;
while ($NUM--) {
    for (@phones) {
        print ++$count, ": ($2) $3-$4\n" if /$re/o and $NUM == 0;
    }
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.