#!perl -w use strict; use 5.010; use Benchmark qw(:all); use File::Map 'map_file'; my $testfile= "$0.testdata"; my $data= '0123456789' x 20e6; # Create the test file. This likely means that it is still hot in the cache... open my $fh, '>', $testfile or die "Couldn't create '$testfile': $!"; print {$fh} $data; undef $fh; sub tr_in_memory { (my $fn)= @_; my $count= ($data =~ tr[0][0]); $count }; sub tr_map_file { (my $fn)= @_; map_file my($content), $testfile; my $count= ($content =~ tr[0][0]); $count }; sub tr_via_readline_10_000 { my $total_filtered = 0; open my $cgs, "<", $testfile; local $/ = \10_000; # blocksize $total_filtered += tr/0/0/ while <$cgs>; }; sub tr_via_readline_100_000 { my $total_filtered = 0; open my $cgs, "<", $testfile; local $/ = \100_000; # blocksize $total_filtered += tr/0/0/ while <$cgs>; }; sub tr_via_readline_1_000_000 { my $total_filtered = 0; open my $cgs, "<", $testfile; local $/ = \1_000_000; # blocksize $total_filtered += tr/0/0/ while <$cgs>; }; say sprintf "Running with a dataset of %d", length $data; cmpthese( 30, { 'tr_map_file' => \&tr_map_file, 'tr_in_memory' => \&tr_in_memory, 'tr_via_readline 10k' => \&tr_via_readline_10_000, 'tr_via_readline 100k' => \&tr_via_readline_100_000, 'tr_via_readline 1m' => \&tr_via_readline_1_000_000, } );