Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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 ()}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Benchmarking regexes
by zwon (Abbot) on Mar 17, 2009 at 23:57 UTC | |
by Anonymous Monk on Mar 18, 2009 at 00:06 UTC | |
|
Re: Benchmarking regexes
by moritz (Cardinal) on Mar 17, 2009 at 23:44 UTC | |
by Anonymous Monk on Mar 17, 2009 at 23:50 UTC | |
|
Re: Benchmarking regexes
by Anonymous Monk on Mar 17, 2009 at 23:43 UTC |