in reply to Interleaving bytes in a string quickly

Inline::C?

Update: Bleh this won't work with nulls because it's returning a vanilla C char*. You'd need to take and return an SV* instead, but you get the idea.

$ perl interleave.plx
0.0027470588684082
0.410820007324219
#!/usr/bin/env perl use strict; use warnings; use Inline 'C'; use Time::HiRes qw( time ); my $buf = chr(1) x 1e6;; my $s = time; my $out = interleave( $buf, chr(0) ); print time() - $s, "\n"; $buf = chr(1) x 1e6;; $s = time; $out = join( chr(0), unpack '(A1)*', $buf ) .chr(0); print time() - $s, "\n"; __END__ __C__ char *interleave(char *in, char other) { char *outbuf = malloc( sizeof(char) * strlen( in ) * 2 + 1); char *outp = outbuf; char *p = in; while( *p ) { *outp++ = *p++; *outp++ = other; } *outp = '\0'; return outbuf; }

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: Interleaving bytes in a string quickly
by ikegami (Patriarch) on Feb 26, 2010 at 18:24 UTC
    Here you go:
    #!/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

    Update: Fixed bugs mentioned elsewhere in thread.