mirror of
https://github.com/remko/waforth
synced 2024-12-27 09:59:29 +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",
|
name: "sieve-js",
|
||||||
fn: () => {
|
fn: () => {
|
||||||
const r = sieveJS(LIMIT);
|
const r = sieveJS(LIMIT);
|
||||||
return r[r.length - 1];
|
return r;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
...(sieveCModule == null
|
...(sieveCModule == null
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
int lastPrime = 0;
|
||||||
for (int i = n; i >= 0; --i) {
|
for (int i = 2; i <= n; ++i) {
|
||||||
if (sieve[i] == 0) {
|
if (!sieve[i]) {
|
||||||
free(sieve);
|
// printf("%d\n", i);
|
||||||
return i;
|
lastPrime = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(sieve);
|
free(sieve);
|
||||||
return 0;
|
return lastPrime;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 = [];
|
|
||||||
for (let i = 2; i <= limit; i++) {
|
|
||||||
nums.push(i);
|
|
||||||
}
|
}
|
||||||
for (let i = 0; i <= sqrtlmt; i++) {
|
const nums = new Uint8Array(n + 1);
|
||||||
const p = nums[i];
|
for (let i = 2; i * i <= n; i++) {
|
||||||
if (p) {
|
if (!nums[i]) {
|
||||||
for (let j = p * p - 2; j < nums.length; j += p) {
|
for (let j = i * i; j <= n; j += i) {
|
||||||
nums[j] = 0;
|
nums[j] = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (let i = 0; i < nums.length; i++) {
|
let lastPrime = 0;
|
||||||
const p = nums[i];
|
for (let i = 2; i < n; i++) {
|
||||||
if (p) {
|
if (!nums[i]) {
|
||||||
primes.push(p);
|
// Print number.
|
||||||
|
// console.log(p);
|
||||||
|
lastPrime = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return lastPrime;
|
||||||
return primes;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)))
|
||||||
|
|
Loading…
Reference in a new issue