1use warnings; 2use strict; 3use File::Temp qw(tempfile tempdir); 4use Test::More tests => 1; 5# Author test, you will need to declare P5LIB to run this test with prove 6use ATWDumbbench; 7use constant BATCH_SIZE => 1000; 8 9my $bench = ATWDumbbench->new( 10 target_rel_precision => 0.005, 11 initial_runs => BATCH_SIZE, 12); 13 14my $dir = tempdir( CLEANUP => 1 ); 15my $template = 'foobar-XXXXXXXX'; 16for ( 1 .. BATCH_SIZE ) { 17 my ( $fh, $filename ) = tempfile( $template, DIR => $dir ); 18 print $fh rand(); 19 close($fh); 20} 21 22my $regex = qr/^\.\.?$/; 23 24$bench->add_instances( 25 Dumbbench::Instance::PerlSub->new( 26 name => 'with grep', 27 code => sub { 28 opendir my $dir_h, $dir or die "Cannot open $dir: $!"; 29 my @top_entries = grep { $_ !~ /^\.\.?$/ } readdir $dir_h; 30 closedir($dir_h); 31 } 32 ), 33 Dumbbench::Instance::PerlSub->new( 34 name => 'with grep and compiled regex', 35 code => sub { 36 opendir my $dir_h, $dir or die "Cannot open $dir: $!"; 37 my @top_entries = grep { $_ !~ $regex } readdir $dir_h; 38 closedir($dir_h); 39 } 40 ), 41 Dumbbench::Instance::PerlSub->new( 42 name => 'with eq', 43 code => sub { 44 opendir my $dir_h, $dir or die "Cannot open $dir: $!"; 45 my @temp = readdir($dir_h); 46 closedir($dir_h); 47 my @top_entries; 48 for (@temp) { 49 next if ( ( $_ eq '.' ) or ( $_ eq '..' ) ); 50 push( @top_entries, $_ ); 51 } 52 } 53 ), 54 Dumbbench::Instance::PerlSub->new( 55 name => 'with enhanced eq', 56 code => sub { 57 opendir my $dir_h, $dir or die "Cannot open $dir: $!"; 58 my @temp = readdir($dir_h); 59 closedir($dir_h); 60 my @top_entries; 61 for (@temp) { 62 next 63 if ( ( length($_) <= 2 ) 64 and ( ( $_ eq '.' ) or ( $_ eq '..' ) ) ); 65 push( @top_entries, $_ ); 66 } 67 } 68 ), 69 Dumbbench::Instance::PerlSub->new( 70 name => 'with enhanced eq mark2', 71 code => sub { 72 opendir my $dir_h, $dir or die "Cannot open $dir: $!"; 73 my @temp = readdir($dir_h); 74 closedir($dir_h); 75 my @top_entries; 76 my $counter = 0; 77 my $found = 0; 78 79 for (@temp) { 80 $counter++; 81 82 if ( ( length($_) <= 2 ) 83 and ( ( $_ eq '.' ) or ( $_ eq '..' ) ) ) 84 { 85 $found++; 86 if ( $found < 2 ) { 87 next; 88 } 89 else { 90 push( @top_entries, splice( @temp, $counter ) ); 91 last; 92 } 93 94 } 95 push( @top_entries, $_ ); 96 } 97 } 98 ), 99 Dumbbench::Instance::PerlSub->new( 100 name => 'with enhanced eq mark3', 101 code => sub { 102 opendir my $dir_h, $dir or die "Cannot open $dir: $!"; 103 my @temp = readdir($dir_h); 104 closedir($dir_h); 105 my ( $first, $second ); 106 my $index = 0; 107 my $found = 0; 108 109 for (@temp) { 110 111 if ( ( length($_) <= 2 ) 112 and ( ( $_ eq '.' ) or ( $_ eq '..' ) ) ) 113 { 114 if ( $found < 1 ) { 115 $first = $index; 116 $found++; 117 $index++; 118 next; 119 } 120 else { 121 $second = $index; 122 last; 123 } 124 125 } 126 else { 127 $index++; 128 } 129 } 130 131 splice( @temp, $first, 1 ); 132 splice( @temp, ( $second - 1 ), 1 ); 133 } 134 ), 135 Dumbbench::Instance::PerlSub->new( 136 name => 'sort and shift', 137 code => sub { 138 opendir( my $dir_h, $dir ) or die "Cannot open $dir: $!"; 139 my @top_entries = readdir($dir_h); 140 closedir($dir_h); 141 @top_entries = sort(@top_entries); 142 143 # removing the '.' and '..' entries 144 shift(@top_entries); 145 shift(@top_entries); 146 } 147 ), 148); 149 150note('This will take a while... hang on.'); 151$bench->run; 152diag( $bench->report_as_text ); 153my $method_name = 'with enhanced eq mark3'; 154 155cmp_ok( 156 $bench->measurements(), 157 '<=', 158 $bench->get_measure($method_name), 159 "'$method_name' is the fastest method." 160); 161