Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Benchmarks aren't everything (rewritten regex is 290% faster than original)

by demerphq (Chancellor)
on Oct 25, 2005 at 08:16 UTC ( [id://502645]=note: print w/replies, xml ) Need Help??


in reply to Re: Benchmarks aren't everything
in thread Benchmarks aren't everything

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://502645]
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: (5)
As of 2024-04-20 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found