Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://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.

---
$world=~s/war/peace/g


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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-29 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found