in reply to Challenge: CPU-optimized byte-wise or-equals (for a meter of beer)
Here is my dirty approach:
use Inline C => qq{ SV* swap(SV* one, SV* two) { char *buf1, *buf2, *buf3; STRLEN idx, len; SV* ret; if (!SvPOK(one) || !SvPOK(two) || sv_len(one) > sv_len(two)) return newSVsv(&PL_sv_undef); len = sv_len(one); buf1 = SvPVX(one); buf2 = SvPVX(two); buf3 = malloc(len); for (idx=0; idx < len; idx++) { buf3[idx] = buf1[idx] ? buf1[idx] : buf2[idx]; } ret = newSVpv(buf3, len); free(buf3); return ret; } }; # [ your code, but with ] cmpthese( -5, { 'split1' => sub { my $s3 = split1( $s1, $s2 ) }, 'substr1' => sub { my $s3 = substr1( $s1, $s2 ) }, 'inline' => sub { my $s3 = swap($s1, $s2) }, });
Which gives, er, some improvement...
Rate split1 substr1 inline split1 4.45/s -- -88% -100% substr1 37.6/s 746% -- -99% inline 6955/s 156241% 18378% --
Although you have to make assumptions about your data, like it not being encoded in utf8. You can test for all this as part of the C, if you need to.
If you need to be this specific about direct byte faffing, always think of Inline::C. Then wrap the dirtyness in a module.
In Section
Seekers of Perl Wisdom