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 @char =
split //,
$text;
# NEW!
my @word =
split /\s+/,
$text;
my @line =
split /^/,
$text;
# 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 /(?:)/, $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 /(?:)/,
$str },
new =>
sub {
split //,
$str },
pack =>
sub {
unpack "(a)*",
$str },
Rate old pack new
old 197/s -- -58% -70%
pack 465/s 136% -- -28%
new 649/s 229% 40% --
The benchmark lies...
...unless you compiled with -DSTUPID_PATTERN_CHECKS
$ perl5.9.5 -Mre=Debug,DUMP -e '//; /(?:)/'
Compiling REx ""
Final program:
1: NOTHING (2)
2: END (0)
minlen 0
Compiling REx "(?:)"
Final program:
1: NOTHING (2)
2: END (0)
minlen 0
split is rarely the bottleneck
old =>
sub { () =
map {
chr }
split /(?:)/,
$str },
new =>
sub { () =
map {
chr }
split //,
$str },
pack =>
sub { () =
unpack "(C)*",
$str },
Rate old new pack
old 81.5/s -- -29% -89%
new 114/s 40% -- -84%
pack 718/s 781% 528% --