Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: regexp find last word

by holli (Abbot)
on Mar 02, 2005 at 14:57 UTC ( [id://435848]=note: print w/replies, xml ) Need Help??


in reply to Re: regexp find last word
in thread regexp find last word

Basically the same principle as my answer, but slightly slower.
use Benchmark; print "holli\n"; timethis (999999, sub { $_ = "fox comes and fox goes into forest"; s/( +fox.+)(fox.+?forest)/${1}the $2/; }); print "borisz\n"; timethis (999999, sub { $_ = "fox comes and fox goes into forest"; s/( +.*)(fox.+?forest)/$1the $2/; }); print "sh1tn\n"; timethis (999999, sub { $_ = "fox comes and fox goes into forest"; s/( +?<=fox)(.+?)(fox)/$1the $2/; }); print "Roy Jonhson\n"; timethis (999999, sub { $_ = "fox comes and fox goes into forest"; s/( +?=fox(?:(?!.*fox).)*forest)/the /; });
prints:
holli timethis 999999: 10 wallclock secs ( 9.87 usr + 0.00 sys = 9.87 CPU) + @ 101275.98/s (n=999999) borisz timethis 999999: 14 wallclock secs (11.08 usr + 0.00 sys = 11.08 CPU) + @ 90268.91/s (n=999999) sh1tn timethis 999999: 12 wallclock secs (10.41 usr + 0.01 sys = 10.42 CPU) + @ 95959.98/s (n=999999) Roy Jonhson timethis 999999: 10 wallclock secs (10.19 usr + 0.02 sys = 10.20 CPU) + @ 98000.69/s (n=999999)
/me wins ;-)


holli, /regexed monk/

Replies are listed 'Best First'.
Re^3: regexp find last word
by Crackers2 (Parson) on Mar 02, 2005 at 15:45 UTC

    That's all well for the speed, but let's check correctness:

    Testing string: fox comes and fox goes into forest

    Holli : fox comes and the fox goes into forest Roy : fox comes and the fox goes into forest Borisz : fox comes and the fox goes into forest shltn : fox comes and the fox goes into forest

    Every solution got that one right.

    Testing string: fox comes fox walks and fox goes into forest

    Holli : fox comes fox walks and the fox goes into forest Roy : fox comes fox walks and the fox goes into forest Borisz : fox comes fox walks and the fox goes into forest shltn : fox comes the fox walks and fox goes into forest

    Three foxes trip up shltn.

    Testing string: pig comes and fox goes into forest

    Holli : pig comes and fox goes into forest Roy : pig comes and the fox goes into forest Borisz : pig comes and the fox goes into forest shltn : pig comes and fox goes into forest

    And just one fox trips up both Holli and shltn

    So I'm just going to pretend I have judging power and disqualify Holli's and shltn's entries, which makes Roy Jonhson the new winner. :)

      Testing string: fox comes fox walks and fox goes into forest. no fox left
      holli: fox comes fox walks and the fox goes into forest. no fox +left borisz: fox comes fox walks and the fox goes into forest. no fox +left sh1tn: fox comes the fox walks and fox goes into forest. no fox +left Roy Johnson: fox comes fox walks and fox goes into forest. no fox left
      And that trips both sh1tn and Roy Johnson. Leaving borisz as the only correct solution.
Re^3: regexp find last word
by Roy Johnson (Monsignor) on Mar 02, 2005 at 16:00 UTC
    With corrected versions of yours and mine, plus ikegami's suggested alteration of mine:
    use strict; use warnings; use Benchmark 'cmpthese'; cmpthese( -2, { holli => sub { $_ = "fox comes and fox goes into forest"; s/(fox.+)?(fox.+?forest)/${1}the $2/; }, Roy => sub { $_ = "fox comes and fox goes into forest"; s/(?=fox(?:(?!fox).)*forest)/the /; }, ikegami => sub { $_ = "fox comes and fox goes into forest"; s/(fox(?:(?!fox).)*forest)/the $1/; }, });
    Rate ikegami holli Roy ikegami 34714/s -- -2% -27% holli 35541/s 2% -- -25% Roy 47659/s 37% 34% --

    Caution: Contents may have been coded under pressure.
Re^3: regexp find last word
by ikegami (Patriarch) on Mar 02, 2005 at 15:45 UTC

    You can remove the (redundant) .* from Roy Johnson's.

    I don't know if it helps any, but you can remove the lookahead too:
    s/(fox(?:(?!fox).)*forest)/the $1/;

      I removed the redundant .* about 2 minutes after I posted it. People see things quick around here.

      Caution: Contents may have been coded under pressure.
Re^3: regexp find last word
by valentin (Abbot) on Mar 02, 2005 at 21:12 UTC
    Here is what I mensure on my PPC:
    use strict; use warnings; use Benchmark 'cmpthese'; cmpthese( -2, { holli => sub { $_ = "fox comes and fox goes into forest"; s/(fox.+)?(fox.+?forest)/${1}the $2/; }, Roy => sub { $_ = "fox comes and fox goes into forest"; s/(?=fox(?:(?!fox).)*forest)/the /; }, ikegami => sub { $_ = "fox comes and fox goes into forest"; s/(fox(?:(?!fox).)*forest)/the $1/; }, borisz => sub { $_ = reverse "fox comes and fox goes into forest"; s/(tserof.*?)xof/${1}xof eht/; $_ = reverse; } } ); __OUTPUT__ Rate holli ikegami Roy borisz holli 62934/s -- -3% -25% -31% ikegami 65121/s 3% -- -23% -29% Roy 84099/s 34% 29% -- -8% borisz 91530/s 45% 41% 9% --

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-03-28 17:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found