You can shave a bit of processing off by slurping a big chunk of data and then splitting at all the included newlines on your own.
while(read $fh, my $buffer, 128*2**10) { $buffer .= <$fh>; # since last line probably crosses buffer border for(split /\n/, $buffer) { # ... } }
You can save more processing time by limiting the record split, so that it knows to stop looking even when it hasn't reached the end of the input string. my @field = split /\|/, $_, 10; # each record has 10 fields Potentially much more efficient is a fairly complicated approach - caveat: your input data must not contain any format errors or it'll completely run wild.
# we assume 10 fields per record again while(read $fh, my $buffer, 128*2**10) { $buffer .= <$fh>; # we dump all records' fields in a big pile, in which # every 9th element contains the last field of one record, # plus a newline, plus the first field of the next record my @in_field_heap = split /\|/, $buffer; while(@in_field_heap) { # pull the two glued fields apart $in_field_heap[9] =~ /^([^\n]*)\n(.*)/; # pull out fields of current record incl the glued one, # and reinject the second half of the double field my @field = splice @in_field_heap, 0, 10, $2; # replace the glued double field by its first half @field[9] = $1; # ... } }
A similar approach comes to mind for your output, but my intuition is entirely undecided on whether it'll run faster or slower.
# ... push @out_field_heap, @field, "\n"; } # single join over the whole batch my $out_buffer = join "|", @out_field_heap; # but that means we surrounded the newlines with pipes, # so fix em $out_buffer =~ s/\|\n\|/\n/g; print OUTPUT $out_buffer; }

Obviously, optimization for speed can decrease your code's legibility and maintainability fast. Be wary of whether you really need it.

Disclaimer: I benchmarked none of these. The read $fh, $buffer, $len; $buffer .= <$fh>; is known to be the fastest block slurping approach however.

If that output acceleration idea works, it might well be applicable to the split acceleration as well.

# 10 fields.. while(read $fh, my $buffer, 128*2**10) { $buffer .= <$fh>; $buffer =~ s/\n/\|/g; my @in_field_heap = split /\|/, $buffer; while(my @field = splice @in_field_heap, 0, 10) { # ... } }
YMMV. Benchmark thoroughly.

Makeshifts last the longest.


In reply to Re: Fastest I/O possible? by Aristotle
in thread Fastest I/O possible? by Anonymous Monk

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.