Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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