Skip to content

Instantly share code, notes, and snippets.

@knutov
Last active November 9, 2022 08:40
Show Gist options
  • Save knutov/8c9077790f925f1e336f to your computer and use it in GitHub Desktop.
Save knutov/8c9077790f925f1e336f to your computer and use it in GitHub Desktop.
perl best slurp

Finding best module to read files in perl

modern server with SSD

prepare test files

< /dev/urandom tr -dc "[:space:][:print:]" | head -c10000 > file.10k
< /dev/urandom tr -dc "[:space:][:print:]" | head -c100000 > file.100k
< /dev/urandom tr -dc "[:space:][:print:]" | head -c1000000 > file.1M

test with 10kb random latin1 text file

perl slurp.pl file.10k 100000

No encodings:
                      Rate IO::All Path::Tiny Perl6 Path::Tiny_raw File::Slurp MikeDoherty dams SublimeSnippet File::Slurper File::Slurp::Tiny File::SlurperB Unix
IO::All            19841/s      --       -55%  -61%           -65%        -81%        -86% -86%           -86%          -88%              -90%           -90% -91%
Path::Tiny         43860/s    121%         --  -14%           -24%        -57%        -69% -70%           -70%          -74%              -78%           -78% -81%
Perl6              51020/s    157%        16%    --           -11%        -51%        -64% -65%           -65%          -69%              -74%           -74% -78%
Path::Tiny_raw     57471/s    190%        31%   13%             --        -44%        -60% -60%           -60%          -66%              -71%           -71% -75%
File::Slurp       103093/s    420%       135%  102%            79%          --        -28% -29%           -29%          -38%              -47%           -47% -56%
MikeDoherty       142857/s    620%       226%  180%           149%         39%          --  -1%            -1%          -14%              -27%           -27% -39%
dams              144928/s    630%       230%  184%           152%         41%          1%   --             0%          -13%              -26%           -26% -38%
SublimeSnippet    144928/s    630%       230%  184%           152%         41%          1%   0%             --          -13%              -26%           -26% -38%
File::Slurper     166667/s    740%       280%  227%           190%         62%         17%  15%            15%            --              -15%           -15% -28%
File::Slurp::Tiny 196078/s    888%       347%  284%           241%         90%         37%  35%            35%           18%                --            -0% -16%
File::SlurperB    196078/s    888%       347%  284%           241%         90%         37%  35%            35%           18%                0%             -- -16%
Unix              232558/s   1072%       430%  356%           305%        126%         63%  60%            60%           40%               19%            19%   --
------------------------------
With utf8:
              Rate      Slurp       Unix Path::Tiny      Perl6    Slurper
Slurp      23148/s         --       -10%       -32%       -45%       -60%
Unix       25773/s        11%         --       -24%       -39%       -55%
Path::Tiny 34014/s        47%        32%         --       -20%       -41%
Perl6      42373/s        83%        64%        25%         --       -26%
Slurper    57471/s       148%       123%        69%        36%         --

test with 100kb random latin1 text file

perl slurp.pl file.100k 100000

No encodings:
                      Rate IO::All Perl6 Path::Tiny Path::Tiny_raw dams MikeDoherty SublimeSnippet File::Slurp File::Slurper File::SlurperB File::Slurp::Tiny Unix
IO::All            12821/s      --  -49%       -58%           -71% -72%        -72%           -78%        -80%          -85%           -86%              -87% -88%
Perl6              25189/s     96%    --       -17%           -43% -44%        -45%           -56%        -61%          -71%           -72%              -74% -77%
Path::Tiny         30211/s    136%   20%         --           -32% -33%        -34%           -48%        -53%          -65%           -67%              -69% -73%
Path::Tiny_raw     44248/s    245%   76%        46%             --  -2%         -3%           -23%        -32%          -48%           -51%              -54% -60%
dams               45249/s    253%   80%        50%             2%   --         -1%           -22%        -30%          -47%           -50%              -53% -59%
MikeDoherty        45662/s    256%   81%        51%             3%   1%          --           -21%        -30%          -47%           -50%              -53% -59%
SublimeSnippet     57803/s    351%  129%        91%            31%  28%         27%             --        -11%          -32%           -36%              -40% -48%
File::Slurp        64935/s    406%  158%       115%            47%  44%         42%            12%          --          -24%           -29%              -32% -42%
File::Slurper      85470/s    567%  239%       183%            93%  89%         87%            48%         32%            --            -6%              -11% -23%
File::SlurperB     90909/s    609%  261%       201%           105% 101%         99%            57%         40%            6%             --               -5% -18%
File::Slurp::Tiny  96154/s    650%  282%       218%           117% 113%        111%            66%         48%           12%             6%                -- -13%
Unix              111111/s    767%  341%       268%           151% 146%        143%            92%         71%           30%            22%               16%   --
------------------------------
With utf8:
              Rate       Unix      Slurp Path::Tiny    Slurper      Perl6
Unix        3007/s         --        -0%       -68%       -73%       -78%
Slurp       3013/s         0%         --       -68%       -73%       -78%
Path::Tiny  9461/s       215%       214%         --       -15%       -29%
Slurper    11173/s       272%       271%        18%         --       -17%
Perl6      13405/s       346%       345%        42%        20%         --
#! /usr/bin/env perl
use v5.14;
use strict;
use warnings;
use Benchmark 'cmpthese';
use File::Slurp qw ( );
use File::Slurp::Tiny qw ( );
use Path::Tiny qw ( );
use File::Slurper qw ( );
use Perl6::Slurp qw ( );
use IO::All;
my $filename = shift or die "No argument given";
my $count = shift || 10;
say "No encodings:";
cmpthese($count, {
'Unix' => sub { open my $fh, '<:unix', $filename or die "Couldn't open $filename: $!"; read $fh, my $buffer, -s $fh or die "Couldn't read $filename: $!" },
'File::Slurp' => sub { File::Slurp::read_file($filename, buffer_ref => \my $buffer, binmode => ':raw') },
'File::Slurper' => sub { File::Slurper::read_text($filename,'latin1') }, # default is utf8
'File::SlurperB' => sub { File::Slurper::read_binary($filename) },
'Path::Tiny' => sub { Path::Tiny::path($filename)->slurp },
'Path::Tiny_raw' => sub { Path::Tiny::path($filename)->slurp_raw },
'File::Slurp::Tiny' => sub { File::Slurp::Tiny::read_file($filename) },
'MikeDoherty' => sub { my $contents = do { local $/; open my $in, '<', $filename; <$in> }; },
'dams' => sub { my $content = do{local(@ARGV,$/)=$filename;<>}; },
'SublimeSnippet' => sub { my $var; { local $/ = undef; local *FILE; open FILE, "<$filename"; $var = <FILE>; close FILE } },
'Perl6' => sub { my $file_contents = Perl6::Slurp::slurp $filename, {raw=>1}; },
'IO::All' => sub { my $file_contents = io->file($filename)->slurp; },
});
# exit;
say "------------------------------";
say "With utf8:";
cmpthese($count, {
'Unix' => sub { open my $fh, '<:utf8', $filename or die "Couldn't open $filename: $!"; read $fh, my $buffer, -s $fh or die "Couldn't read $filename: $!" },
'Slurp' => sub { File::Slurp::read_file($filename, buffer_ref => \my $buffer, binmode => ':utf8') },
'Slurper' => sub { File::Slurper::read_text($filename,'utf8') },
'Path::Tiny' => sub { Path::Tiny::path($filename)->slurp_utf8 },
'Perl6' => sub { my $file_contents = Perl6::Slurp::slurp $filename, {utf8=>1}; },
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment