|
Revision 362, 0.6 kB
(checked in by JarrettBillingsley, 3 months ago)
|
Fixed a bug where array data was being allocated but not initialized, causing some GC cycles to try to scan garbage data (and crash). Updated the benchmarks and all that I've tested have worked fine, not to mention being at least 15% faster in most cases :)
|
| Line | |
|---|
| 1 |
module benchmark.nsieve |
|---|
| 2 |
|
|---|
| 3 |
// n = 9, 13.79 sec |
|---|
| 4 |
// laptop: 7.718 sec (rather nice) |
|---|
| 5 |
|
|---|
| 6 |
function nsieve(m) |
|---|
| 7 |
{ |
|---|
| 8 |
local isPrime = array.new(m, true) |
|---|
| 9 |
local count = 0 |
|---|
| 10 |
|
|---|
| 11 |
for(i: 2 .. m) |
|---|
| 12 |
{ |
|---|
| 13 |
if(isPrime[i]) |
|---|
| 14 |
{ |
|---|
| 15 |
for(k : i + i .. m, i) |
|---|
| 16 |
isPrime[k] = false |
|---|
| 17 |
|
|---|
| 18 |
count++ |
|---|
| 19 |
} |
|---|
| 20 |
} |
|---|
| 21 |
|
|---|
| 22 |
return count |
|---|
| 23 |
} |
|---|
| 24 |
|
|---|
| 25 |
function main(N) |
|---|
| 26 |
{ |
|---|
| 27 |
local n = 9 |
|---|
| 28 |
|
|---|
| 29 |
if(isString(N)) |
|---|
| 30 |
try n = toInt(N); catch(e) {} |
|---|
| 31 |
|
|---|
| 32 |
local timer = time.Timer.clone() |
|---|
| 33 |
timer.start() |
|---|
| 34 |
|
|---|
| 35 |
for(i: 0 .. 3) |
|---|
| 36 |
{ |
|---|
| 37 |
local m = 10000 << (n - i) |
|---|
| 38 |
writefln("Primes up to {,8} {,8}", m, nsieve(m)) |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
timer.stop() |
|---|
| 42 |
writefln("Took {} sec", timer.seconds()) |
|---|
| 43 |
} |
|---|