1769416 in Rate by_regex by_index by_memchr by_regex 51.2/s -- -3% -68% by_index 52.9/s 3% -- -67% by_memchr 162/s 217% 207% -- #### use strict; use warnings; use Benchmark qw( cmpthese ); my $fn = '/boot/vmlinux-2.6.18.8-96-default.gz'; open my $fh, '<', $fn or die $!; read $fh, my $buffer, 2_000_000 or die $!; print length $buffer, " in\n"; close $fh; my $subs = { by_index => sub { my ($p0, @offs)=(-1, ()); push @offs, $p0 while +($p0=index $buffer, "\xaa", $p0+1) != -1; push @offs, $p0 while +($p0=index $buffer, "\xbb", $p0+1) != -1; push @offs, $p0 while +($p0=index $buffer, "\xcc", $p0+1) != -1; return 0 + @offs }, by_regex => sub { my @offs=(); push @offs, pos($buffer) while $buffer =~ /\xaa/g; push @offs, pos($buffer) while $buffer =~ /\xbb/g; push @offs, pos($buffer) while $buffer =~ /\xcc/g; return 0 + @offs }, by_memchr => sub { my @offs=(); my_memchr( \@offs, $buffer, "\xaa" ); my_memchr( \@offs, $buffer, "\xbb" ); my_memchr( \@offs, $buffer, "\xcc" ); return 0 + @offs } }; cmpthese -3, $subs; use Inline C => qq[ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ void my_memchr(SV* rvav, SV* sv, SV *ch) { STRLEN srclen; char byte = *SvPV(ch, PL_na) ; char *svc = SvPV(sv, srclen); char *p = svc, *end = svc + srclen; AV *av = (AV*)SvRV(rvav); // if(SvTYPE(SvRV(rvav)) == SVt_PVAV) while((p=memchr(p, (int)byte, end-p)) !=0 && p