in reply to Speed reading (files)

Not that this really did ever care much in most practical situations I've faced myself (so that I most commonly stick with local $/;), but I have often seen mentioned that File::Slurp provides much optimized file slurping facilities.

Replies are listed 'Best First'.
Re^2: Speed reading (files)
by kwaping (Priest) on Aug 04, 2005 at 15:49 UTC
    I too have read about File::Slurp and how it's supposed to be super fast. However, check this out:
    using read function 0.684900999069214 0.681570053100586 0.680304050445557 0.675194025039673 0.684563159942627 0.686581134796143 File::Slurp 1.57559299468994 1.5706889629364 1.5739688873291 1.5618691444397 1.56290698051453 1.58691692352295
    That's using the same test as above, replacing
    open(FILE,"<$file") || die $!; my $data = do { local $/; <FILE> }; close(FILE);
    with
    my $data = read_file($file);

      Have you seen the article in the File::Slurp distribution? At it's core, it's doing something very similar to the "read" approach. I'd guess the speed difference you're seeing is due to the option/error/context checking that it's doing, which your hand-coded example is not.

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.