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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.