NOTHING is faster in 5.10
Ævar Arnfjörð Bjarmason
split optimizations
Handled by custom C code in pp_split
. Will never reach the regex
engine (see perlreapi.pod)
my @line =
split /^/,
$text;
my @word =
split /\s+/,
$text;
my @char =
split //,
$text;
# NEW!
# Hands in the air if you know what this does
my @what =
split ' ',
$text;
The benchmark
use Benchmark ':all';
my $str = ("hlagh" x 666);
cmpthese(-1, {
old => sub { split / /x, $str },
new => sub { split //, $str },
pack => sub { unpack "(a)*", $str },
});
Benchmark results
The new split //
is around 3x faster than it was in 5.8.8
old =>
sub {
split / /x,
$str },
new =>
sub {
split //,
$str },
pack =>
sub {
unpack "(a)*",
$str },
Rate old pack new
old 182/s -- -61% -72%
pack 473/s 160% -- -28%
new 661/s 262% 40% --
The benchmark lies...
...unless you compiled with -DSTUPID_PATTERN_CHECKS
./Configure -A ccflags=-DSTUPID_PATTERN_CHECKS
$ perl5.9.5 -Mre=Debug,DUMP -e '//; /(?#I CAN HAS OPTIMIZE?!)/'
Compiling REx ""
Final program:
1: NOTHING (2)
2: END (0)
minlen 0
Compiling REx "(?#I CAN HAS OPTIMIZE?!)"
Final program:
1: NOTHING (2)
2: END (0)
minlen 0
split is rarely the bottleneck
old =>
sub { () =
map {
chr }
split /(?:)/x,
$str },
new =>
sub { () =
map {
chr }
split //,
$str },
pack =>
sub { () =
unpack "(C)*",
$str },
Rate old new pack
old 80.7/s -- -31% -89%
new 117/s 45% -- -84%
pack 725/s 798% 518% --