Last active
December 31, 2015 00:39
-
-
Save kgoess/957c9320361c8a0c2832 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat long-test.pl | |
use strict; | |
use warnings; | |
use Benchmark; | |
# build a regex with a lot of alternations | |
my $r = ''; | |
for my $l ('a'..'y', 'A'..'Y'){ | |
$r .= $l x 10; | |
$r .= '|'; | |
} | |
$r .= 'sanfrancisco\.com'; | |
# build a long string | |
my $check_body = 'Z' x 14_000_000; | |
$check_body .= 'sanfrancisco.com'; | |
# time them, with qr// and without | |
my $count = 1; | |
timethese($count, { | |
without_qr => sub { testme( $r ) }, | |
with_qr => sub { testme( qr/$r/ ) }, | |
}); | |
sub testme { | |
my $text_block_string = shift; | |
if ($check_body =~ /($text_block_string)/ig) { | |
print "match! $1\n"; | |
}else{ | |
print "no match :-( \n"; | |
} | |
pos($check_body) = 0; | |
} | |
------------------------------------------------------- | |
$ perl long-test.pl | |
Benchmark: timing 1 iterations of with_qr, without_qr... | |
match! sanfrancisco.com | |
with_qr: 0 wallclock secs ( 0.01 usr + 0.00 sys = 0.01 CPU) @ 100.00/s (n=1) | |
(warning: too few iterations for a reliable count) | |
match! sanfrancisco.com | |
without_qr: 15 wallclock secs (15.21 usr + 0.00 sys = 15.21 CPU) @ 0.07/s (n=1) | |
(warning: too few iterations for a reliable count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment