Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

As a follow up here is a benchmark of the regex as posted against the equivelent regex that uses classes and not alternation, note the drammatic difference in run time. ($rex1 is the regex without alternations, $rex2 is the regex as posted, with newlines inserted to make it moderately easier to read)

use Benchmark qw(cmpthese timethese); $rex1=qr! (<[^/]([^>]*[^/>])?>)|(</[^>]*>)|(<[^>]*/>)| ( [\p{Lu}\p{Ll}\p{Lt}\p{Nd}\p{Nl}\p{No}\x{4e00}-\x{9fa5}\x{3007}\x{3 +021}-\x{3029}] (?: [-\p{Lu}\p{Ll}\p{Lt}\p{Nd}\p{Nl}\p{No}._:''\x{4e00}-\x{9fa5}\x +{3007}\x{3021}-\x{3029}]* [\p{Lu}\p{Ll}\p{Lt}\p{Nd}\p{Nl}\p{No}\x{4e00}-\x{9fa5}\x{3007} +\x{3021}-\x{3029}] )? )!x; $rex2=qr!(<[^/]([^>]*[^/>])?>)|(</[^>]*>)|(<[^>]*/>)| ((\p{Lu}|\p{Ll}|\p{Lt}|\p{Nd}|\p{Nl}|\p{No}|[\x{4e00}-\x{9fa5}]|\x{300 +7} |[\x{3021}-\x{3029}])((\p{Lu}|\p{Ll}|\p{Lt}|\p{Nd}|\p{Nl}|\p{No}|[-._: +''] |[\x{4e00}-\x{9fa5}]|\x{3007}|[\x{3021}-\x{3029}])*(\p{Lu}|\p{Ll}|\p{L +t} |\p{Nd}|\p{Nl}|\p{No}|[\x{4e00}-\x{9fa5}]|\x{3007}|[\x{3021}-\x{3029}] +))?)!x; $html=get_text(); # fix this! my $test={'rex1'=>'$count1=0; $count1++ while $html=~/$rex1/g;', 'rex2'=>'$count2=0; $count2++ while $html=~/$rex2/g;',}; timethese(-1,$test); cmpthese(-1,$test); print "$count1\n$count2\n"; sub get_text { return <<'EOFTEXT'; *** PUT SOME HTML/XML HERE *** EOFTEXT }

which has the following output:

Benchmark: running rex1, rex2 for at least 1 CPU seconds... rex1: 1 wallclock secs ( 1.16 usr + 0.00 sys = 1.16 CPU) @ 51 +.04/s (n=59) rex2: 1 wallclock secs ( 1.08 usr + 0.00 sys = 1.08 CPU) @ 12 +.99/s (n=14) Rate rex2 rex1 rex2 13.0/s -- -74% rex1 50.6/s 290% -- 6291 6291

So by removing the alternation we see a 290% speedup. What was the run time difference in the benchmark being referenced? Well apparently Java took 244 seconds and Perl took 527, which afaict (my maths arent so hot) is %115 faster. Now perhaps Java also would benefit from this modest optimisation of his code, but perhaps not, perhaps the Java implementation does this internally and in fact would not see an improvement. Either way the case does not seem to be proved at all.

BTW, for my benchmarks i just used the source for a page of Perlmonks. Use whatever XML/HTML you like.


In reply to Re^2: Benchmarks aren't everything (rewritten regex is 290% faster than original) by demerphq
in thread Benchmarks aren't everything by tilly

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (8)
    As of 2018-05-24 18:45 GMT
    Find Nodes?
      Voting Booth?