Skip to content

Instantly share code, notes, and snippets.

@sugar84
Created June 30, 2011 19:49
Show Gist options
  • Save sugar84/1057050 to your computer and use it in GitHub Desktop.
Save sugar84/1057050 to your computer and use it in GitHub Desktop.
Perl test measurement
#!/usr/bin/env perl
use strict;
use warnings;
# use integer;
sub get_primes7 {
my $n = shift;
if ($n < 2) { return (); }
if ($n == 2) { return (2); }
my @s;
push @s, $_*2 + 1 for 1..$n/2;
my $mroot = $n ** 0.5;
my $half = scalar @s;
my $i = 0;
my $m = 3;
while ($m <= $mroot) {
if ($s[$i]) {
my $j = ($m*$m - 3) / 2;
$s[$j] = 0;
while ($j < $half) {
$s[$j] = 0;
$j += $m;
}
}
$i = $i + 1;
$m = 2*$i + 3;
}
return [2, grep {$_ != 0} @s];
}
my $res;
for (1..10) {
$res = get_primes7(10000000);
print "Found " . scalar @$res . " prime numbers.\n";
}
__END__
$ /usr/bin/time ./math_test.pl
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
Found 664579 prime numbers.
37.87user 0.15system 0:38.02elapsed 99%CPU
with 'use integer':
34.31user 0.13system 0:34.46elapsed 99%CPU
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment