Created
March 29, 2018 00:35
-
-
Save samcv/adc8d50df303e457ed7cb6b85b66e94e 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
use nqp; | |
my @a; | |
my Str $haystack = slurp 'tolstoy.txt'; | |
add-to-list("index with single codepoint needle", test-index($haystack, "\x[01]")); | |
my $target = 'abdicatez'; | |
add-to-list("index with word needle", test-index($haystack, $target)); | |
my $new-haystack; | |
for $haystack.comb(($haystack.chars / 20).Int) { | |
$new-haystack ~= $_; | |
} | |
$haystack = $new-haystack; | |
add-to-list("index with word needle STRAND", test-index($haystack, $target)); | |
add-to-list("regex INDEXINGOPT", test-regex($haystack, rx/$target/)); | |
$haystack = nqp::indexingoptimized($haystack); | |
add-to-list("index with word needle INDEXINGOPT", test-index($haystack, $target)); | |
print-array @a; | |
exit; | |
sub test-index (Str:D $Haystack, Str:D $needle, Int:D $times = 100) { | |
my str $H = $Haystack; | |
my str $n = $needle; | |
my $t1 = now; | |
nqp::index($H, $n, 0) for ^$times; | |
my $t2 = now; | |
$t2 - $t1; | |
} | |
sub test-regex (Str:D $Haystack, $regex, Int:D $times = 1) { | |
my $t1 = now; | |
my $result; | |
($result = $Haystack ~~ $regex) for ^$times; | |
die if $result; | |
my $t2 = now; | |
$t2 - $t1; | |
} | |
sub add-to-list (Str:D $name, $time) { | |
@a.push: Pair.new($name, $time); | |
} | |
sub print-array(@a) { | |
@a.join("\n").say; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment