#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); my $file='test.txt'; my $file_size=63*1024*1024; # 60MB my $iter=10; my $size=0; $size = -s $file if(-f $file); $/="\x0A"; print "use file $file with $file_size Byte\n"; # create file if($size != $file_size) { my $lines=int($file_size/81); print "create File ($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)).$/; } my $diff=$file_size-($lines*81); while($diff>0) { print $fh chr(int(rand(95))+32); $diff--; } close($fh); } else { print "use existing File\n"; } # loding file in OS-Filesystem-Cache: print "preload file\n"; open(my $gfh, '<', $file) or die("$file : $!\n"); { local $/=undef; <$gfh>; } print "Run Benchmark\n"; cmpthese($iter, { 'read all' => sub{ open(my $fh, '<', $file) or die("$file : $!\n"); my @l=<$fh>; close($fh); }, 'read push' => sub{ open(my $fh, '<', $file) or die("$file : $!\n"); my @l; while(<$fh>) { push(@l,$_); } close($fh); }, 'read all seek' => sub{ # goto file start seek($gfh,0,0); my @l=<$gfh>; }, 'read push seek' => sub{ # goto file start seek($gfh,0,0); my @l; while(<$gfh>) { push(@l,$_); } }, });