my %hash; @hash{0, 15, 16, 31} = (); # then add this to your benchmark hash => sub { while ( $data =~ /^(\d+) (\d+)/mg ) { next if exists $hash{$1} or exists $hash{$2}; return 1; } }, #### Rate any_cr any ugly ugly_cr hash2 hash any_cr 865/s -- -37% -54% -65% -100% -100% any 1382/s 60% -- -27% -44% -100% -100% ugly 1896/s 119% 37% -- -24% -100% -100% ugly_cr 2489/s 188% 80% 31% -- -100% -100% hash 3084047/s 356493% 222992% 162532% 123813% 26% --