http://www.perlmonks.org?node_id=502645


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