#!/usr/bin/perl -w use strict; use warnings; use List::Util qw(first); use Benchmark; my %test; for my $j (1970..2020) { for my $m (1..12) { for my $t (1..31) { my $d = sprintf '%02d.%02d.%d', $t, $m, $j; $test{$d}++; } } } my %found; my $m_grep = sub { foreach my $m (1..12) { if (grep { /\.0?$m\./ } keys %test) { $found{$m} ||= 1; } } }; my $m_first = sub { foreach my $m (1..12) { if (first { /\.0?$m\./ } keys %test) { $found{$m} ||= 1; } } }; my $m_pq = sub { my %notfound; @notfound{1..12} = (); my $re = join "|", keys %notfound; for my $key (keys %test) { if ($key =~ m/\.0?($re)\./) { my $month = $1 + 0; $found{$month} ||= 1; delete $notfound{$month}; $re = join "|", keys %notfound; # wenn alle gefunden, braucht man gar nicht mehr suchen last unless keys %notfound; } } }; timethese(100, { 'grep' => $m_grep, 'first' => $m_first, 'pq' => $m_pq });