my @arr = unpack('A8A8A8A8A8A8A8A8A8A8A8A8A8A8A8A8A8A8A8A8', $str);
####
# No longer wins but is still faster than '(A8)*'
my @arr = unpack((join '', ('A8' x ($strlen / 8))), $str);
##
##
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark 'cmpthese';
my $str = "abcdefgh12345678" x 2000;
my $strlen = length $str;
my $end = $strlen / 8 - 1;
my $fmt = join '', ('A8' x ($strlen / 8));
my @pos = map {$_ * 8} 0 .. $end;
cmpthese(-3, {
'regex' => sub { my @arr = $str =~ /(........)/g;},
'split_grep' => sub { my @arr = grep $_, split /(.{8})/, $str;},
'split_pos' => sub { my @arr = split /(?(?{pos() % 8})(?!))/, $str;},
'substr_map' => sub { my @arr = map substr($str, $_, 8), @pos;},
'substr_for' => sub { my @arr; push @arr, substr($str, $_, 8) for @pos;},
'unpack' => sub { my @arr = unpack($fmt, $str);}
});
__DATA__
Rate split_pos split_grep substr_map unpack substr_for
split_pos 38.2/s -- -28% -54% -57% -74%
split_grep 53.1/s 39% -- -36% -41% -64%
regex 77.4/s 110% 52% -- -3% -12% -46%
substr_map 82.3/s 115% 55% -- -8% -45%
unpack 89.5/s 134% 69% 9% -- -40%
substr_for 149/s 291% 182% 81% 67% --