diff options
| author | Bob Lied <boblied+github@gmail.com> | 2024-06-12 19:38:18 -0500 |
|---|---|---|
| committer | Bob Lied <boblied+github@gmail.com> | 2024-06-12 19:38:18 -0500 |
| commit | e3d7d9d87f2799af27557a4f0e2aa36e84057ae1 (patch) | |
| tree | b889ee58233244d3cc3dc73eba39259b4342e7f9 | |
| parent | 53dad097e142b9e98929709397867ab1cbbfcd08 (diff) | |
| download | perlweeklychallenge-club-e3d7d9d87f2799af27557a4f0e2aa36e84057ae1.tar.gz perlweeklychallenge-club-e3d7d9d87f2799af27557a4f0e2aa36e84057ae1.tar.bz2 perlweeklychallenge-club-e3d7d9d87f2799af27557a4f0e2aa36e84057ae1.zip | |
Benchmark index vs RE in task 2
| -rw-r--r-- | challenge-273/bob-lied/perl/ch-2.pl | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/challenge-273/bob-lied/perl/ch-2.pl b/challenge-273/bob-lied/perl/ch-2.pl index 817a23bef8..7132fa848a 100644 --- a/challenge-273/bob-lied/perl/ch-2.pl +++ b/challenge-273/bob-lied/perl/ch-2.pl @@ -17,11 +17,12 @@ use v5.40; use Getopt::Long; -my $Verbose = 0; -my $DoTest = 0; +my $DoTest = false; +my $Benchmark = 0; -GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +GetOptions("test" => \$DoTest, "benchmark:i" => \$Benchmark); exit(!runTest()) if $DoTest; +exit( runBenchmark($Benchmark) ) if $Benchmark; say ( bAfterA($_) ? "true" : "false" ) for @ARGV; @@ -31,6 +32,11 @@ sub bAfterA($str) return $w >= 0 && index($str, "a", $w) < 0; } +sub bAfterA_RE($str) +{ + $str =~ m/^[^b]*b[^a]*$/ +} + sub runTest { use Test2::V0; @@ -40,5 +46,24 @@ sub runTest is (bAfterA("aaa" ), false, "Example 3"); is (bAfterA("bbb" ), true, "Example 4"); + is (bAfterA_RE("aabb"), true, "Example 1 RE"); + is (bAfterA_RE("abab"), false, "Example 2 RE"); + is (bAfterA_RE("aaa" ), false, "Example 3 RE"); + is (bAfterA_RE("bbb" ), true, "Example 4 RE"); + done_testing; } + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + + cmpthese( $repeat, { + index => sub{ + bAfterA("aabb"), bAfterA("abab"), bAfterA("aaaa"), bAfterA("bbbb"), + }, + regex => sub{ + bAfterA_RE("aabb"), bAfterA_RE("abab"), bAfterA_RE("aaaa"), bAfterA_RE("bbbb"), + }, + }); +} |
