Hello,

I'm looking for a faster way to de-interleave binary data. I have some source files (~200MB in size) of interleaved data that contain two data streams. The data is interleaved one bit at a time (e.g., if stream one is all 1's and stream two is all 0's, the file would read as "1010101010101010....", and I would like the resultant files to read "1111111..." and "000000...").

The code below works, but is slow (so it seems to me - it takes about 40 minutes to de-interleave a 192MB file on a modern laptop). I read in some data, split each bit into an array, the split that array into two sub arrays, then write it back out. I'm a newbie to Perl and programming in general, which may be obvious from my code.

until ($filesize <= $offset) { sysseek (INTERLEAVED_DATA, $offset, 0); sysread (INTERLEAVED_DATA, $buffer, 8192); @interleaved = split(//, unpack("b*", $buffer)); $buffer = ""; while (@interleaved){ push @uninterleaved_1, shift @interleaved; push @uninterleaved_2, shift @interleaved; } $uninterleaved_1 = pack "b*", join "", @uninterleaved_1; @uninterleaved_1 = ""; $uninterleaved_2 = pack "b*", join "", @uninterleaved_2; @uninterleaved_2 = ""; syswrite (UNINTERLEAVED_1, $uninterleaved_1); syswrite (UNINTERLEAVED_2, $uninterleaved_2); $uninterleaved_1 = ""; $uninterleaved_2 = ""; $offset = $offset + 8192; }
I've tried reading in bigger chunks of data, but it doesn't help. It seems it's all the unpacking and array operations that take the most time. Thanks.

In reply to de-interleaving binary data by joe_b

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.