Skip to content

Instantly share code, notes, and snippets.

@lestrrat
Created April 26, 2011 16:12
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 lestrrat/942564 to your computer and use it in GitHub Desktop.
Save lestrrat/942564 to your computer and use it in GitHub Desktop.
# http://www.ebisawa.co.jp/wiki/index.php?Perl%2F%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E6%8C%87%E5%90%91
use strict;
use Benchmark qw(cmpthese);
# use Perl6::Slurp;
use File::Slurp;
cmpthese(
-1,
{
"File::Slurp" => sub {
my $indexFile = __FILE__;
my @array = read_file ( $indexFile, { chomp => 1 } );;
},
"mapChomp1" => sub {
my $indexFile = __FILE__;
my @array;
open( my $fh, $indexFile ) || die( "can't open index file: $!" );
map { chomp; push( @array, $_ ); } <$fh>;
close $fh;
},
"mapChomp2" => sub {
my $indexFile = __FILE__;
open( my $fh, $indexFile ) || die( "can't open index file: $!" );
my @array = map { chomp; $_; } <$fh>;
close $fh;
},
"mapChomp3" => sub {
my $indexFile = __FILE__;
open( my $fh, $indexFile ) || die( "can't open index file: $!" );
my @array = map { local $/; chomp; $_; } <$fh>;
close $fh;
},
split => sub {
my $indexFile = __FILE__;
open( my $fh, $indexFile ) || die( "can't open index file: $!" );
my @array = split /\n/, do { local $/; <$fh> };
close $fh;
},
}
);
__END__
daisuke@beefcake ~$ perl read.pl
Rate File::Slurp mapChomp3 mapChomp2 mapChomp1 split
File::Slurp 5023/s -- -31% -53% -65% -70%
mapChomp3 7319/s 46% -- -31% -49% -56%
mapChomp2 10676/s 113% 46% -- -26% -36%
mapChomp1 14354/s 186% 96% 34% -- -14%
split 16747/s 233% 129% 57% 17% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment