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.while(read $fh, my $buffer, 128*2**10) { $buffer .= <$fh>; # since last line probably crosses buffer border for(split /\n/, $buffer) { # ... } }
A similar approach comes to mind for your output, but my intuition is entirely undecided on whether it'll run faster or slower.# 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; # ... } }
# ... 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.
YMMV. Benchmark thoroughly.# 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) { # ... } }
Makeshifts last the longest.
In reply to Re: Fastest I/O possible?
by Aristotle
in thread Fastest I/O possible?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |