#!/usr/bin/env perl use strict; use warnings; use Inline C => <<'__EOI__'; /* sv_in and sv_pad may not be NULL */ /* That won't happen when called from Perl */ SV* interleave_bytes(SV* sv_in, SV* sv_pad) { STRLEN l_in; char* p_in = SvPVbyte(sv_in, l_in); STRLEN l_pad; char* p_pad = SvPVbyte(sv_pad, l_pad); char pad; SV* sv_out; char* p_out; if (l_pad != 1) croak("usage"); pad = *p_pad; sv_out = newSVpvn("", 0); p_out = SvGROW(sv_out, l_in*2+1); /* XXX Could overflow */ SvCUR_set(sv_out, l_in*2); while (l_in--) { *(p_out++) = *(p_in++); *(p_out++) = pad; } *p_out = '\0'; return sv_out; } __EOI__ use Time::HiRes qw( time ); my $buf = chr(1) x 1e6;; my $s = time; my $out = interleave_bytes( $buf, chr(0) ); print time() - $s, "\n"; #use Devel::Peek; #Dump $out; $buf = chr(1) x 1e6;; $s = time; $out = join( chr(0), unpack '(A1)*', $buf ) .chr(0); print time() - $s, "\n"; #### 0.00459790229797363 0.446739912033081