Sieve benchmark tweaks

This commit is contained in:
Remko Tronçon 2022-12-27 20:32:13 +01:00
parent 8b85b7f787
commit 7cb569c146
4 changed files with 31 additions and 44 deletions

View file

@ -70,7 +70,7 @@ const benchmarks = [
name: "sieve-js", name: "sieve-js",
fn: () => { fn: () => {
const r = sieveJS(LIMIT); const r = sieveJS(LIMIT);
return r[r.length - 1]; return r;
}, },
}, },
...(sieveCModule == null ...(sieveCModule == null

View file

@ -5,25 +5,23 @@ int sieve(int n) {
if (n < 2) { if (n < 2) {
return n; return n;
} }
int m = (int)sqrt((double)n);
char *sieve = calloc(n + 1, sizeof(char)); char *sieve = calloc(n + 1, sizeof(char));
sieve[0] = 1; sieve[0] = 1;
sieve[1] = 1; sieve[1] = 1;
for (int i = 2; i <= m; i++) { for (int i = 2; i * i <= n; i++) {
if (!sieve[i]) { if (!sieve[i]) {
for (int j = i * i; j <= n; j += i) { for (int j = i * i; j <= n; j += i) {
if (!sieve[j]) { sieve[j] = 1;
sieve[j] = 1;
}
} }
} }
} }
for (int i = n; i >= 0; --i) { int lastPrime = 0;
if (sieve[i] == 0) { for (int i = 2; i <= n; ++i) {
free(sieve); if (!sieve[i]) {
return i; // printf("%d\n", i);
lastPrime = i;
} }
} }
free(sieve); free(sieve);
return 0; return lastPrime;
} }

View file

@ -1,27 +1,24 @@
// Source: https://rosettacode.org/wiki/Sieve_of_Eratosthenes#JavaScript // Source: https://rosettacode.org/wiki/Sieve_of_Eratosthenes#JavaScript
export default function sieve(limit) { export default function sieve(n) {
const primes = []; if (n < 2) {
if (limit >= 2) { return n;
const sqrtlmt = Math.sqrt(limit) - 2; }
const nums = []; const nums = new Uint8Array(n + 1);
for (let i = 2; i <= limit; i++) { for (let i = 2; i * i <= n; i++) {
nums.push(i); if (!nums[i]) {
} for (let j = i * i; j <= n; j += i) {
for (let i = 0; i <= sqrtlmt; i++) { nums[j] = 1;
const p = nums[i];
if (p) {
for (let j = p * p - 2; j < nums.length; j += p) {
nums[j] = 0;
}
}
}
for (let i = 0; i < nums.length; i++) {
const p = nums[i];
if (p) {
primes.push(p);
} }
} }
} }
return primes; let lastPrime = 0;
for (let i = 2; i < n; i++) {
if (!nums[i]) {
// Print number.
// console.log(p);
lastPrime = i;
}
}
return lastPrime;
} }

View file

@ -4,24 +4,16 @@
(local $i i32) (local $i i32)
(local $j i32) (local $j i32)
(local $last i32) (local $last i32)
(memory.fill (i32.const 0) (i32.const 0) (local.get $n))
(local.set $i (i32.const 0))
(block $endLoop
(loop $loop
(br_if $endLoop (i32.ge_s (local.get $i) (local.get $n)))
(i32.store8 (local.get $i) (i32.const 1))
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(br $loop)))
(local.set $i (i32.const 2)) (local.set $i (i32.const 2))
(block $endLoop (block $endLoop
(loop $loop (loop $loop
(br_if $endLoop (i32.ge_s (i32.mul (local.get $i) (local.get $i)) (local.get $n))) (br_if $endLoop (i32.ge_s (i32.mul (local.get $i) (local.get $i)) (local.get $n)))
(if (i32.eq (i32.load8_s (local.get $i)) (i32.const 1)) (if (i32.eqz (i32.load8_s (local.get $i)))
(then (then
(local.set $j (i32.mul (local.get $i) (local.get $i))) (local.set $j (i32.mul (local.get $i) (local.get $i)))
(loop $innerLoop (loop $innerLoop
(i32.store8 (local.get $j) (i32.const 0)) (i32.store8 (local.get $j) (i32.const 1))
(local.set $j (i32.add (local.get $j) (local.get $i))) (local.set $j (i32.add (local.get $j) (local.get $i)))
(br_if $innerLoop (i32.lt_s (local.get $j) (local.get $n)))))) (br_if $innerLoop (i32.lt_s (local.get $j) (local.get $n))))))
(local.set $i (i32.add (local.get $i) (i32.const 1))) (local.set $i (i32.add (local.get $i) (i32.const 1)))
@ -29,7 +21,7 @@
(local.set $i (i32.const 2)) (local.set $i (i32.const 2))
(loop $loop (loop $loop
(if (i32.eq (i32.load8_s (local.get $i)) (i32.const 1)) (if (i32.eqz (i32.load8_s (local.get $i)))
(then (then
(local.set $last (local.get $i)))) (local.set $last (local.get $i))))
(local.set $i (i32.add (local.get $i) (i32.const 1))) (local.set $i (i32.add (local.get $i) (i32.const 1)))