mirror of
https://github.com/remko/waforth
synced 2025-01-18 22:26:39 +01:00
Extract sieve benchmark
This commit is contained in:
parent
df64428f77
commit
bebe495332
6 changed files with 84 additions and 24 deletions
8
Makefile
8
Makefile
|
@ -21,6 +21,14 @@ tests: $(WASM_FILES)
|
|||
sieve-vanilla: $(WASM_FILES)
|
||||
$(PARCEL) --no-hmr -o dist/sieve-vanilla.html tests/benchmarks/sieve-vanilla/index.html
|
||||
|
||||
.PHONY: benchmark-sieve
|
||||
benchmark-sieve: $(WASM_FILES)
|
||||
$(PARCEL) build -o dist/benchmark-sieve.html tests/benchmarks/sieve/index.html
|
||||
|
||||
.PHONY: benchmark-sieve-dev
|
||||
benchmark-sieve-dev: $(WASM_FILES)
|
||||
$(PARCEL) --no-hmr tests/benchmarks/sieve/index.html
|
||||
|
||||
wasm: $(WASM_FILES) src/tools/quadruple.wasm.hex
|
||||
|
||||
dist/waforth.wasm: src/waforth.wat dist
|
||||
|
|
|
@ -2,33 +2,11 @@ import "whatwg-fetch";
|
|||
import "promise-polyfill/src/polyfill";
|
||||
import $ from "jquery";
|
||||
import WAForth from "./WAForth";
|
||||
import sieve from "./sieve";
|
||||
|
||||
window.jQuery = $;
|
||||
require("jq-console");
|
||||
|
||||
// Copied from https://rosettacode.org/wiki/Sieve_of_Eratosthenes#Forth
|
||||
const sieve = `
|
||||
: prime? HERE + C@ 0= ;
|
||||
: composite! HERE + 1 SWAP C! ;
|
||||
|
||||
: sieve
|
||||
HERE OVER ERASE
|
||||
2
|
||||
BEGIN
|
||||
2DUP DUP * >
|
||||
WHILE
|
||||
DUP prime? IF
|
||||
2DUP DUP * DO
|
||||
I composite!
|
||||
DUP +LOOP
|
||||
THEN
|
||||
1+
|
||||
REPEAT
|
||||
DROP
|
||||
1 SWAP 2 DO I prime? IF DROP I THEN LOOP .
|
||||
;
|
||||
`;
|
||||
|
||||
const forth = new WAForth();
|
||||
|
||||
let jqconsole = $("#console").jqconsole("WAForth\n", "");
|
||||
|
|
22
src/shell/sieve.js
Normal file
22
src/shell/sieve.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
// Copied from https://rosettacode.org/wiki/Sieve_of_Eratosthenes#Forth
|
||||
export default `
|
||||
: prime? HERE + C@ 0= ;
|
||||
: composite! HERE + 1 SWAP C! ;
|
||||
|
||||
: sieve
|
||||
HERE OVER ERASE
|
||||
2
|
||||
BEGIN
|
||||
2DUP DUP * >
|
||||
WHILE
|
||||
DUP prime? IF
|
||||
2DUP DUP * DO
|
||||
I composite!
|
||||
DUP +LOOP
|
||||
THEN
|
||||
1+
|
||||
REPEAT
|
||||
DROP
|
||||
1 SWAP 2 DO I prime? IF DROP I THEN LOOP .
|
||||
;
|
||||
`;
|
|
@ -32,7 +32,7 @@
|
|||
(define !returnStackBase #x4000)
|
||||
(define !stackBase #x10000)
|
||||
(define !dictionaryBase #x20000)
|
||||
(define !memorySize (* 1 1024 1024))
|
||||
(define !memorySize (* 100 1024 1024))
|
||||
|
||||
(define !moduleHeader
|
||||
(string-append
|
||||
|
|
10
tests/benchmarks/sieve/index.html
Normal file
10
tests/benchmarks/sieve/index.html
Normal file
|
@ -0,0 +1,10 @@
|
|||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title></title>
|
||||
</head>
|
||||
<body>
|
||||
<script src="./index.js"></script>
|
||||
</body>
|
||||
</html>
|
42
tests/benchmarks/sieve/index.js
Normal file
42
tests/benchmarks/sieve/index.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
import WAForth from "../../../src/shell/WAForth";
|
||||
import sieve from "../../../src/shell/sieve";
|
||||
|
||||
const ITERATIONS = 3;
|
||||
const LIMIT = 50000000;
|
||||
|
||||
const forth = new WAForth();
|
||||
let outputBuffer = [];
|
||||
forth.onEmit = c => {
|
||||
outputBuffer.push(String.fromCharCode(c));
|
||||
};
|
||||
document.body.innerHTML = "Loading...";
|
||||
forth.start().then(
|
||||
() => {
|
||||
document.body.innerHTML = "<div>Running...</div>";
|
||||
forth.run(sieve);
|
||||
let i = 0;
|
||||
const run = () => {
|
||||
if (i < ITERATIONS) {
|
||||
outputBuffer = [];
|
||||
const t1 = performance.now();
|
||||
outputBuffer = [77, 88];
|
||||
forth.run(`${LIMIT} sieve`);
|
||||
const t2 = performance.now();
|
||||
document.body.innerHTML =
|
||||
document.body.innerHTML +
|
||||
`<div><pre style='display: inline-block; margin: 0; margin-right: 1rem'>${outputBuffer.join(
|
||||
""
|
||||
)}</pre><span>${(t2 - t1) / 1000.0}</span></div>`;
|
||||
i += 1;
|
||||
window.setTimeout(run, 0);
|
||||
} else {
|
||||
document.body.innerHTML = document.body.innerHTML + "<div>Done</div>";
|
||||
}
|
||||
};
|
||||
window.setTimeout(run, 10);
|
||||
},
|
||||
err => {
|
||||
console.error(err);
|
||||
document.body.innerHTML = "Error";
|
||||
}
|
||||
);
|
Loading…
Reference in a new issue