in reply to Faster and more efficient way to read a file vertically

If "same length", then straightforward and perhaps not perlish, and idea originated before Discipulus's answer :). I wonder how inefficient this is compared to slurping/reading in large blocks, i.e. if read and seek 'cooperate' on input buffer (I don't know enough on underlying C calls).

use strict; use warnings; use autodie; my $POS = 10; open my $fh, '<', 'dna.txt'; my $L = length( <$fh> ) - 1; seek $fh, $POS - 1, 0; my ( $s, $i ) = ( '', 0 ); seek $fh, $L, 1 while read $fh, $s, 1, $i++; print "$s\n";

Replies are listed 'Best First'.
Re^2: Faster and more efficient way to read a file vertically
by ForgotPasswordAgain (Vicar) on Nov 03, 2017 at 20:58 UTC
    FWIW, seek was my first thought, too. (Also that I'd prototype in Perl, then write the same thing in C. I might've found my weekend project... :) I can't imagine that allocating memory is going to help (I like when my imagination is challenged, though). I think at least if we can assume the file is in filesystem cache the read will be coming from RAM already anyway.
      I think this is parallelizable, too. If you have 24 cores, you can seek to $L/24, do your thing, combine results.