in reply to Re: Challenge: CPU-optimized byte-wise or-equals (for a meter of beer)
in thread Challenge: CPU-optimized byte-wise or-equals (for a meter of beer)

I was curious - my "goal" when jotting down the code was to use the minimal number of Perl ops and to let Perl do as much work as possible in the C parts of Perl. So I returned a list from map instead of returning a string:

This returns the list and joins all the fragments later:

sub map_split { my $ofs = 0; join "", map { $ofs += length; $_ => substr $s2, $ofs++, 1 } split + /\0/, $s1, -1; };

This does a partial fragment concatenation in the map and returns a single string, but still does the concatenation later, but of half as many elements - one additional op in the map call:

sub map_split_join { my $ofs = 0; join "", map { $ofs += length; $_ . substr $s2, $ofs++, 1 } split +/\0/, $s1, -1; };

I didn't expect the difference to be that big:

Rate map_split_join subst map_split map_split_join 1697/s -- -7% -13% subst 1828/s 8% -- -6% map_split 1949/s 15% 7% --

So, I can only recommend reading/listening to When Perl Is Not Quite Fast Enough. Undoubtedly, there is still lots of potential in optimizing this.