Rate grepmethod deletemethod grepmethod 1.99/s -- -99% deletemethod 270/s 13432% -- #### #! perl -sw use strict; use vars qw($count $hsize $nsize $ndsize $i1 $i2 %remove); use Benchmark 'cmpthese'; local $\=$/; print 'Setting up test data'; my ($count, $hsize, $nsize, $ndsize) = ($::count||100, $::hsize||28000, $::nsize||1000, int($::nsize||1000/10)); #! Array of values to be removed - used in all tests. my @needles = (0 .. $nsize, 0 .. $ndsize); #! Some stuff to find and remove #!------------------ Grep method setup ------------------------------------------ #! Lookup arrays for grep method. my @grepHaystacks; print 'Creating GrepHaystacks'; push @grepHaystacks, [1 .. $hsize] for 0 .. $count+1; sub grepMethod{ $remove{$_}=1 for @needles; @{$grepHaystacks[$i1]} = grep !$remove{$_}, @{$grepHaystacks[$i1]}; $i1++; #! test overhead } #!------------------- Delete method setup---------------------------------------- #! Lookup hashes for delete method my @deleteHaystacks; print 'Creating Delete Haystacks'; my %hash; keys %hash=$hsize; @hash{1 .. $hsize} = undef; #! The assignment is a syntactic req. only for (0 .. $count+1) { local $\; my %temphash; keys %temphash = 100; %temphash = %hash; push @deleteHaystacks, \%temphash; print "\r$_"; #! Feel good that something is happening. } print $/; sub deleteMethod{ delete @{$deleteHaystacks[$i2]}{@needles}; $i2++; #! Test overhead } #! Check the algorithms are equivalent $i1 = $i2 = 0; print 'Lookup list size before grepmethod: ', scalar @{$grepHaystacks[0]}; grepMethod(); print 'Lookup list size after grepmethod: ', scalar @{$grepHaystacks[0]}; print 'Lookup list size before del method: ', scalar keys %{$deleteHaystacks[0]}; deleteMethod(); print 'Lookup list size after del method: ', scalar keys %{$deleteHaystacks[0]}; cmpthese( $count, { grepmethod => \&grepMethod, deletemethod=> \&deleteMethod, }); __END__ c:\test>207632-4 -count=100 -hsize=28000 -nsize=1000 -ndsize=100 Setting up test data Creating GrepHaystacks Creating Delete Haystacks 101 Lookup list size before grepmethod: 28000 Lookup list size after grepmethod: 27000 Lookup list size before del method: 28000 Lookup list size after del method: 27000 Benchmark: timing 100 iterations of deletemethod, grepmethod ... deletemethod: 0 wallclock secs ( 0.37 usr + 0.00 sys = 0.37 CPU) @ 269.54/s (n=100) (warning: too few iterations for a reliable count) grepmethod: 68 wallclock secs (49.11 usr + 1.09 sys = 50.20 CPU) @ 1.99/s (n=100) Rate grepmethod deletemethod grepmethod 1.99/s -- -99% deletemethod 270/s 13432% -- c:\test>