roughly 5-7 times faster!

$mult=1e7; $buf = chr(1) x $mult; $s = time; $out = join( chr(0), unpack '(A1)*', $buf ) .chr(0); print "TIME0:",time() - $s,"\n"; $del=pack('(B16)*','00000000'.'11111111')x $mult; $buf="ABCDEFGHIJ"x($mult/10); $s = time(); #$buf2=pack('(B16)*',unpack('(B8)*',$buf)); $buf2=pack 'v*', unpack 'C*', $buf; # jmcnamaras pack-unpac +k-ing is much faster !!! $out = $del|$buf2; print "TIME1:",time() - $s,"\n"; print join ",",unpack '(B8)20',$out; # check result
OUT:

TIME0:14 TIME1:2 01000001,11111111,01000010,11111111,01000011,11111111,01000100,1111111 +1,01000101,11111111,01000110,11111111,01000111,11111111,01001000,1111 +1111,01001001,11111111,01001010,11111111

Cheers Rolf

UPDATE: even if it's a one-time task you can profit from this approach by chunking the string into eqally sized pieces which are "or"ed against the precalculated $del string.

UPDATE: even if you include calculating $del it's about 3 times faster!


In reply to Re^2: Interleaving bytes in a string quickly by LanX
in thread Interleaving bytes in a string quickly by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.