Skip to content

Instantly share code, notes, and snippets.

@dagolden
Created May 17, 2014 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dagolden/973bcfa11816e2d96c65 to your computer and use it in GitHub Desktop.
Save dagolden/973bcfa11816e2d96c65 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use v5.10;
use strict;
use warnings;
use Benchmark qw( cmpthese );
use Path::Tiny;
use File::Slurp qw/read_file/;
sub fast_slurp {
my ($file) = @_;
open my $fh, "<:unix", $file or die $!;
my $size = -s $fh;
my $buf;
read $fh, $buf, $size; # File::Slurp in a nutshell
return $buf;
}
for my $f (qw/foo-small foo-med foo-big/) {
say "\nBENCHMARKING $f";
my $path = path($f);
my $count = -1;
cmpthese(
$count,
{
'Path::Tiny' => sub { my $raw = $path->slurp_raw },
'File::Slurp' => sub { my $raw = read_file( $f, binmode => ':raw' ) },
'fast slurp' => sub { my $raw = fast_slurp( $f ) },
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment