diff --git a/src/web/benchmarks/benchmarks.js b/src/web/benchmarks/benchmarks.js index 17886ae..83f8d5e 100644 --- a/src/web/benchmarks/benchmarks.js +++ b/src/web/benchmarks/benchmarks.js @@ -70,7 +70,7 @@ const benchmarks = [ name: "sieve-js", fn: () => { const r = sieveJS(LIMIT); - return r[r.length - 1]; + return r; }, }, ...(sieveCModule == null diff --git a/src/web/benchmarks/sieve/sieve.c b/src/web/benchmarks/sieve/sieve.c index a6bda85..cea6b40 100644 --- a/src/web/benchmarks/sieve/sieve.c +++ b/src/web/benchmarks/sieve/sieve.c @@ -5,25 +5,23 @@ int sieve(int n) { if (n < 2) { return n; } - int m = (int)sqrt((double)n); char *sieve = calloc(n + 1, sizeof(char)); sieve[0] = 1; sieve[1] = 1; - for (int i = 2; i <= m; i++) { + for (int i = 2; i * i <= n; i++) { if (!sieve[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) { - if (sieve[i] == 0) { - free(sieve); - return i; + int lastPrime = 0; + for (int i = 2; i <= n; ++i) { + if (!sieve[i]) { + // printf("%d\n", i); + lastPrime = i; } } free(sieve); - return 0; + return lastPrime; } diff --git a/src/web/benchmarks/sieve/sieve.js b/src/web/benchmarks/sieve/sieve.js index dc91605..4d385a6 100644 --- a/src/web/benchmarks/sieve/sieve.js +++ b/src/web/benchmarks/sieve/sieve.js @@ -1,27 +1,24 @@ // Source: https://rosettacode.org/wiki/Sieve_of_Eratosthenes#JavaScript -export default function sieve(limit) { - const primes = []; - if (limit >= 2) { - const sqrtlmt = Math.sqrt(limit) - 2; - const nums = []; - for (let i = 2; i <= limit; i++) { - nums.push(i); - } - for (let i = 0; i <= sqrtlmt; i++) { - 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); +export default function sieve(n) { + if (n < 2) { + return n; + } + const nums = new Uint8Array(n + 1); + for (let i = 2; i * i <= n; i++) { + if (!nums[i]) { + for (let j = i * i; j <= n; j += i) { + nums[j] = 1; } } } - return primes; + let lastPrime = 0; + for (let i = 2; i < n; i++) { + if (!nums[i]) { + // Print number. + // console.log(p); + lastPrime = i; + } + } + return lastPrime; } diff --git a/src/web/benchmarks/sieve/sieve.wat b/src/web/benchmarks/sieve/sieve.wat index f70400a..43ca25b 100644 --- a/src/web/benchmarks/sieve/sieve.wat +++ b/src/web/benchmarks/sieve/sieve.wat @@ -4,24 +4,16 @@ (local $i i32) (local $j i32) (local $last i32) - - (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))) - + (memory.fill (i32.const 0) (i32.const 0) (local.get $n)) (local.set $i (i32.const 2)) (block $endLoop (loop $loop (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 (local.set $j (i32.mul (local.get $i) (local.get $i))) (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))) (br_if $innerLoop (i32.lt_s (local.get $j) (local.get $n)))))) (local.set $i (i32.add (local.get $i) (i32.const 1))) @@ -29,7 +21,7 @@ (local.set $i (i32.const 2)) (loop $loop - (if (i32.eq (i32.load8_s (local.get $i)) (i32.const 1)) + (if (i32.eqz (i32.load8_s (local.get $i))) (then (local.set $last (local.get $i)))) (local.set $i (i32.add (local.get $i) (i32.const 1)))