mirror of
https://github.com/remko/waforth
synced 2024-12-25 09:59:07 +01:00
Sieve benchmark tweaks
This commit is contained in:
parent
8b85b7f787
commit
7cb569c146
4 changed files with 31 additions and 44 deletions
|
@ -70,7 +70,7 @@ const benchmarks = [
|
|||
name: "sieve-js",
|
||||
fn: () => {
|
||||
const r = sieveJS(LIMIT);
|
||||
return r[r.length - 1];
|
||||
return r;
|
||||
},
|
||||
},
|
||||
...(sieveCModule == null
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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)))
|
||||
|
|
Loading…
Reference in a new issue