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.
In reply to Re: Interleaving bytes in a string quickly
by Fletch
in thread Interleaving bytes in a string quickly
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |