Skip to content

Instantly share code, notes, and snippets.

/bench.pl Secret

Created June 5, 2015 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/323642c4e82a750a99b4 to your computer and use it in GitHub Desktop.
Save anonymous/323642c4e82a750a99b4 to your computer and use it in GitHub Desktop.
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