Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Matching date range with pure regex

by MCS (Monk)
on Feb 17, 2004 at 15:04 UTC ( [id://329615]=note: print w/replies, xml ) Need Help??


in reply to Matching date range with pure regex

Kudos to the voters... awarding Abigail (with the slowest regex) the most number of votes. (at least as I write this) There is a difference between wanting to do it with a regex and wanting to do it as slowly as possible. I took the above regex's from grinder, roger, posix_guy, and Abigail and ran them through a benchmark doing 10,000 iterations each.

First, Roger: might want to add a $ on the end there because yours matches 19534 and 20010 etc... and posix_guy: yours matched 19534 for some reason. Anyway, I took grinder's data set and put it in an array (as with the multiple subroutines all accessing __DATA__ it wasn't properly displaying things). Then I seperated each regex into a different subroutine with the name of the author. I've attached the code if anyone wants to run it themselves. All results are from my Powerbook G4 667Mhz.

Benchmark: timing 10000 iterations of abigail, grinder, posixguy, roger...
grinder: 1 wallclock secs ( 1.06 usr + 0.03 sys = 1.09 CPU) @ 9174.31/s (n=10000)
roger: 2 wallclock secs ( 1.02 usr + 0.07 sys = 1.09 CPU) @ 9174.31/s (n=10000)
posixguy: 1 wallclock secs ( 1.37 usr + 0.03 sys = 1.40 CPU) @ 7142.86/s (n=10000)
abigail: 57 wallclock secs (51.45 usr + 0.38 sys = 51.83 CPU) @ 192.94/s (n=10000)

#!/usr/bin/perl -w use strict; use Benchmark; my @data = qw{1949 1950 1951 1999 2000 2001 2010 2049 2050 2051 2102 2 +2102 19534 19080 20010}; sub grinder() { foreach( @data ) { chomp; print "$_ ", /^(19[5-9]\d|20([0-4]\d|50))$/ ? "ok\n" : "n +ok\n"; } } sub roger() { foreach (@data) { chomp; print "$_ ", /^(?:19|20)(?:(?:(?<=19)[5-9]|(?<=20)[0-4 +])[0-9]|50)/x ? "Ok\n" : "not ok\n"; } } sub posixguy() { foreach (@data) { chomp; print "$_ ", /^(19[5-9]\d)|(20([0-4]\d)|50)$/ ? "ok\n" + : "nok\n"; } } sub abigail() { foreach (@data) { chomp; local $" = "|"; print "$_ ", /^(?:@{[1950 .. 2050]})$/ ? "ok\n" : "nok +\n"; } } timethese(10000, {grinder=>'grinder()', roger=>'roger()', posixguy=>'p +osixguy()', abigail=>'abigail()'});

Replies are listed 'Best First'.
Re: Re: Matching date range with pure regex
by MCS (Monk) on Feb 17, 2004 at 21:55 UTC

    Just for kicks, I removed the print statement (as suggested by grinder) and threw in a ++$foo instead. Got the following: (Abigail's is still last but has the most ammount of votes still...)

    Benchmark: timing 10000 iterations of abigail, grinder, posixguy, roge +r... abigail: 55 wallclock secs (51.42 usr + 0.12 sys = 51.54 CPU) @ 19 +4.02/s (n=10000) grinder: 1 wallclock secs ( 0.60 usr + 0.00 sys = 0.60 CPU) @ 16 +666.67/s (n=10000) posixguy: 1 wallclock secs ( 0.96 usr + 0.00 sys = 0.96 CPU) @ 10 +416.67/s (n=10000) roger: 1 wallclock secs ( 0.61 usr + 0.00 sys = 0.61 CPU) @ 16 +393.44/s (n=10000)

    Updated code is in a readmore block (I removed the prints and added a $)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-03-28 19:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found