#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); my $file='test.txt'; my $file_size=60*1024*1024; # 60MB my $iter=10; # create file { my $lines=int($file_size/81); print "create file $file ($file_size Byte) with $lines Lines and 81 chars per line\n"; open(my $fh, '>', $file) or die("$file : $!\n"); for(1..$lines) { print $fh join('',map{chr(int(rand(94))+32)}(1..80))."\n"; } close($fh); } # open only once open(my $fh, '<', $file) or die("$file : $!\n"); # loding file in OS-Filesystem-Cache: { local $/=undef; <$fh>; } print "Run Benchmark\n"; cmpthese($iter, { 'read all' => sub{ # goto file start seek($fh,0,0); my @l=<$fh>; }, 'read push' => sub{ # goto file start seek($fh,0,0); my @l; while(<$fh>) { push(@l,$_); } }, }); close($fh);