use strict; use warnings; use Benchmark 'cmpthese'; my %subs = ( list_io => sub { # Second time round this will take # a loooooong time. my $file=$0; my @line; open my($fh), $file; @line= <$fh>; close($fh); return \@line; }, split_slurp => sub { my $file=$0; my @line; local $/; open my($fh), $file; @line=split /\n/,<$fh>; close($fh); return \@line; }, while_io => sub { my $file=$0; my @line; open my($fh), $file; push @line,$_ while <$fh>; close($fh); return \@line; }, ); cmpthese -5,\%subs; __END__ #### Benchmark: running list_io, split_slurp, while_io, each for at least 5 CPU seconds... list_io: 5 wallclock secs ( 3.89 usr + 1.34 sys = 5.24 CPU) @ 1721.98/s (n=9018) split_slurp: 4 wallclock secs ( 3.02 usr + 2.21 sys = 5.23 CPU) @ 2725.38/s (n=14251) while_io: 7 wallclock secs ( 3.64 usr + 1.38 sys = 5.02 CPU) @ 1629.26/s (n=8174) Rate while_io list_io split_slurp while_io 1629/s -- -5% -40% list_io 1722/s 6% -- -37% split_slurp 2725/s 67% 58% --