Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Question on Regex

by Anonymous Monk
on Nov 18, 2012 at 14:26 UTC ( #1004417=note: print w/replies, xml ) Need Help??


in reply to Re: Question on Regex
in thread Question on Regex

Thanks Athanasius. I got what I was looking. Thanks a lot. :)

Replies are listed 'Best First'.
Re^3: Question on Regex
by karlgoethebier (Prior) on Nov 18, 2012 at 16:41 UTC

    FYI:

    #!/usr/bin/perl use strict; use warnings; use Benchmark qw ( :hireswallclock cmpthese timethese ); our $string = qq (.co.uk/Jobs/Company-Sector/C8A6446X4PND86M9WYJ/Trade +wind/?APath=2.21.0.0.0); sub karlgoethebier { our $string; $string =~ m/.+\/.+\/.+\/.+\/(.+)\/.+/; return $1; } sub athanasius { our $string; return (split '/', $string)[4]; } my $results = timethese (-10, { 'karlgoethebier' => 'karlgoethebier', 'athanasius' => 'athanasius', }); cmpthese($results); __END__ Karls-Mac-mini:Desktop karl$ ./tradewind.pl Benchmark: running athanasius, karlgoethebier for at least 10 CPU seco +nds... athanasius: 10.4769 wallclock secs (10.47 usr + 0.00 sys = 10.47 CPU) + @ 627362.46/s (n=6568485) karlgoethebier: 10.4287 wallclock secs (10.42 usr + 0.00 sys = 10.42 +CPU) @ 105188.77/s (n=1096067) Rate karlgoethebier athanasius karlgoethebier 105189/s -- -83% athanasius 627362/s 496% --

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Which shows that sub athanasius is up to 5 times faster than sub karlgoethebier. Even easier to see when supplying a positive COUNT value to timethese:

      #! perl use strict; use warnings; use Benchmark qw( :hireswallclock cmpthese timethese ); my $string = ".co.uk/Jobs/Company-Sector/C8A6446X4PND86M9WYJ/Tradewind +/?APath=2.21.0.0.0"; cmpthese( timethese ( 1_000_000, { 'karlgoethebier' => sub { $string =~ m/.+\/.+\/.+\/.+\/(.+)\/.+/; retu +rn $1; }, 'athanasius' => sub { return (split '/', $string)[4] }, } ) );

      Output:

      13:09 >perl 390_SoPW.pl Benchmark: timing 1000000 iterations of athanasius, karlgoethebier... athanasius: 5.19894 wallclock secs ( 5.15 usr + 0.00 sys = 5.15 CPU) + @ 194250.19/s (n=1000000) karlgoethebier: 20.2485 wallclock secs (20.08 usr + 0.00 sys = 20.08 +CPU) @ 49808.24/s (n=1000000) Rate karlgoethebier athanasius karlgoethebier 49808/s -- -74% athanasius 194250/s 290% -- 13:16 >

      Not really surprising, since regexen with quantifiers can be expensive:

      Avoid regular expressions with many quantifiers.... Such patterns can result in exponentially slow backtracking behavior unless the quantified subpatterns match on their first “pass”.
      The Camel Book, 4th Edition, p. 693.

      So, what happens if we limit the backtracking?

      #! perl use strict; use warnings; use Benchmark qw( :hireswallclock cmpthese timethese ); my $string = ".co.uk/Jobs/Company-Sector/C8A6446X4PND86M9WYJ/Tradewind +/?APath=2.21.0.0.0"; cmpthese( timethese ( 1_000_000, { 'karlgoethebier' => sub { $string =~ m/.+\/.+\/.+\/.+\/(.+)\/.+/; retu +rn $1; }, 'karlgoethebier2' => sub { $string =~ m/.+?\/.+?\/.+?\/.+?\/(.+?)\/.+/; + return $1; }, 'athanasius' => sub { return (split '/', $string)[4] }, } ) );

      Result:

      13:28 >perl 390_SoPW.pl Benchmark: timing 1000000 iterations of athanasius, karlgoethebier, ka +rlgoethebier2... athanasius: 4.91799 wallclock secs ( 4.88 usr + 0.00 sys = 4.88 CPU) + @ 204792.14/s (n=1000000) karlgoethebier: 20.1721 wallclock secs (20.05 usr + 0.00 sys = 20.05 +CPU) @ 49885.26/s (n=1000000) karlgoethebier2: 2.55908 wallclock secs ( 2.53 usr + 0.00 sys = 2.53 + CPU) @ 395569.62/s (n=1000000) Rate karlgoethebier athanasius karlgoethebie +r2 karlgoethebier 49885/s -- -76% -8 +7% athanasius 204792/s 311% -- -4 +8% karlgoethebier2 395570/s 693% 93% +-- 13:29 >perl 390_SoPW.pl

      The regex is now significantly faster than split-with-subscript. Interesting!

      Athanasius <°(((><contra mundum

        Thats's nice! It's not a good habit to be too greedy. Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004417]
help
Chatterbox?
[james28909]: do you relize how shrodinger felt when he was describing the quantum world? what about neils bohr?
[holli]: it's probably the autist in you. I am like that too, no fucks given about what others think about me
james28909 checks, yep... htere are no fucks to give
[james28909]: another thing i am sick of is hurting others feelings
[1nickt]: james28909 You lost me a bit when you said "show me evolution from intelligence to ignorance". I guess we would have to define the terms, but as I look around at my species and how we are destroying our habitat ...
[james28909]: seems like a person cant even have a belief without hurting someones feelings. i never said my way is the only way. i just said i have pretty good scientific evidence, and then asked for opposing side to present evidence
[james28909]: was downvoted and left without a reply. go figure
[holli]: but then you DO give a fuck
[1nickt]: I don;t think beliefs should be down-voted, just behaviours.
[james28909]: im not quite how to explain it any better nick. you evolved from ignorance to intelligence. not the other way. the universe evolves from gas coulds and debris into planets stars and galaxies ect. it doesnt happen any other way. hence it has ....

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (13)
As of 2017-12-15 14:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (433 votes). Check out past polls.

    Notices?