In my quest to compare equivalent regular expressions, I have attempted to reduce duplicate code by moving the "testing" to a subroutine. The odd thing is that when moved into a subroutine, alternation takes about the same time as when using character classes.
-------------use strict; use Time::HiRes 'time'; sub main { my $TimesToDo = 1000; my $TestString ="abababdedfg" x 1000; my $Count = $TimesToDo; my $StartTime = time(); while ($Count-- > 0) { $TestString =~m/^(a|b|c|d|e|f|g)+$/; } my $EndTime = time(); printf("Alternation takes %.3f seconds.\n", $EndTime - $StartTime) +; $Count = $TimesToDo; $StartTime = time(); while ($Count-- > 0) { $TestString =~m/^[a-g]+$/; } $EndTime = time(); printf("Character class %.3f seconds.\n", $EndTime - $StartTime); } unless (caller) {main ()}
use strict; use Time::HiRes 'time'; #TimesToDo, TestString, Regex sub test { my $TimesToDo = shift; my $TestString = shift() x 1000; my $Count = $TimesToDo; my $StartTime = time(); while ($Count-- > 0) { $TestString =~m/^$_[2]+$/; } my $EndTime = time(); return $EndTime - $StartTime; } sub main { my $result = test(1000,"abababdedfg","(a|b|c|d|e|f|g)"); printf("Alternation takes %.3f seconds.\n", $result); $result = test(1000,"abababdedfg","[a-g]"); printf("Character class %.3f seconds.\n", $result); } unless (caller) {main ()}
In reply to Benchmarking regexes by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |