-
-
Save anonymous/323642c4e82a750a99b4 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 strict; | |
use warnings; | |
use Benchmark 'cmpthese'; | |
my @az = ( 'a' .. 'z', ( '.', ' ' ) x 5, "\n" ); | |
my $str = join '', map $az[ @az * rand ], 1 .. 1 << 16; | |
printf "lenght: %d\n", length $str; | |
cmpthese - 1, { | |
an1 => sub { | |
my ( $i, @result ) = 0; | |
while (1) { | |
my $letter = unpack "x${i}a", $str; | |
$letter or last; | |
$letter eq "\n" | |
? do { | |
pos($str) = ++$i; | |
$str =~ /\G(\w+)/o | |
and ( push( @result, [ $i => $1 ] ), $i += length $1, ); | |
} | |
: ( $i++ ); | |
} | |
}, | |
an2 => sub { | |
my ( $i, @result ) = 0; | |
while (1) { | |
my $letter = substr $str, $i, 1; | |
$letter or last; | |
$letter eq "\n" | |
? do { | |
pos($str) = ++$i; | |
$str =~ /\G(\w+)/o | |
and ( push( @result, [ $i => $1 ] ), $i += length $1, ); | |
} | |
: ( $i++ ); | |
} | |
}, | |
an3 => sub { | |
my ( $i, @result ) = 0; | |
while (1) { | |
my $letter = vec $str, $i, 8; | |
$letter or last; | |
$letter == 0x0A | |
? do { | |
pos($str) = ++$i; | |
$str =~ /\G(\w+)/o | |
and ( push( @result, [ $i => $1 ] ), $i += length $1, ); | |
} | |
: ( $i++ ); | |
} | |
}, | |
ban => sub { | |
my ( $i, @result ) = 0; | |
while () { | |
last if ( $i = index $str, "\n", $i ) < 0; | |
pos $str = ++$i; | |
if ( $str =~ /\G(\w+)/o ) { | |
push @result, [ $i, $1 ]; | |
$i += length $1; | |
} | |
} | |
}, | |
reg => sub { | |
my @result; | |
while ( $str =~ /\n(\w+)/gso ) { | |
push @result, [ pos($str) - length($1), $1 ]; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment