in reply to Most efficient way to load file contents into scalar?

File::Slurp uses sysread, thus bypassing any IO layers, which can be a performance benefit.
  • Comment on Re: Most efficient way to load file contents into scalar?

Replies are listed 'Best First'.
Re^2: Most efficient way to load file contents into scalar?
by citromatik (Curate) on Apr 24, 2009 at 11:32 UTC

    A direct sysread call seems to be >2x faster:

    $ ls -sh bigfile.txt 2.8G bigfile.txt $ cat direct_sysread.pl use Fcntl qw(:DEFAULT); use Symbol; my $file = shift @ARGV; my $fh = gensym; sysopen $fh, $file, O_RDONLY; my $content; sysread $fh, $content, -s $file; $ cat file_slurp.pl use File::Slurp; my $file = shift @ARGV; open my $fh, "<", $file or die $!; my $content = read_file ($file); $ time direct_sysread.pl bigfile.txt real 0m1.785s user 0m0.008s sys 0m1.776s $ time file_slurp.pl bigfile.txt real 0m4.994s user 0m0.924s sys 0m4.052s

    citromatik