use Modern::Perl; use Time::HiRes qw/time/; my $start = time; my $top = 550; my $max = 100; my @primes; my $prime; for ( 2 .. $top ) { $prime = $_; next if $primes[$_]; last unless --$max; { local $_; $primes[ $_ * $prime ] = 1 for 2 .. $top / $prime; } } my $found = time; say $_ for ( grep { !$primes[$_] } 2 .. $prime ); my $end = time; say sprintf "Found all: %.6f seconds", ( $found - $start ); say sprintf "Printing all: %.6f seconds", ( $end - $found ); say sprintf "Running time: %.6f seconds", ( $end - $start ); #### 2 3 5 7 11 13 17 19 23 29 31 ... 491 499 503 509 521 523 541 Found all: 0.000692 seconds Printing all: 0.000879 seconds Running time: 0.001571 seconds