-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
executable file
·173 lines (144 loc) · 4.27 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html>
<head>
<title>N-body system - AssemblyScript</title>
<link rel="icon" href="http://assemblyscript.org/favicon.ico" type="image/x-icon" />
<meta name="viewport" content="user-scalable=0" />
<style>
html,
body {
height: 100%;
margin: 0;
overflow: hidden;
color: #111;
background: #fff;
font-family: sans-serif;
}
body {
border-top: 2px solid #070809;
}
h1 {
padding: 18px 20px 20px;
font-size: 12pt;
margin: 0;
}
a {
color: #111;
text-decoration: none;
}
a:hover {
color: #efbd03;
text-decoration: underline;
}
</style>
</head>
<body>
<h1>
<a href="https://en.wikipedia.org/wiki/N-body_problem">N-body system</a> in
<a href="https://emscripten.org/">C + Emscripten</a>,
<a href="http://assemblyscript.org">AssemblyScript</a>,
vanilla JS,
<a href="https://www.rust-lang.org/">Plain Rust + WASM target</a>,
<a href="https://rustwasm.github.io/wasm-pack/">Rust + wasm-pack</a>
</h1>
<div id="print"></div>
<script src="build/index.browser.js"></script>
<script src="build/index.asm.browser.js"></script>
<script src="c/nbody.js"></script>
<script>
"use strict";
const MagicNumberMemory = 17;
function loadWASMModule(path) {
return fetch(path)
.then(response => response.arrayBuffer())
.then(buffer => WebAssembly.instantiate(buffer, {
env: {
memory: new WebAssembly.Memory({ initial: MagicNumberMemory }),
abort: (filename, line, column) => {
throw Error("abort called at " + line + ":" + colum);
},
log: (s) => console.log(s)
}
}))
.then(module => {
var exports = module.instance.exports;
return exports;
}).catch(err => {
alert("Failed to load WASM: '" + path + "' because " + err.message + " (ad blocker, maybe?)");
console.log(err.stack);
});
}
function getAssemblyScriptWASM() {
return loadWASMModule("build/optimized.wasm")
}
function getRustWASM() {
return loadWASMModule("build/rust.wasm")
}
function getRustWASMWebPack() {
return loadWASMModule("build/rust_nbody_wasmpack_bg.wasm")
}
// JS version is already loaded as JSwrapper
// ASM version is already loaded as MyAsmModule
// C version is already loaded as Module
function runCWasm(cb) {
return Module().then((exports) => cb({
init: exports._init,
bench: exports._bench,
step: exports._step
}))
}
function print(string) {
const el = document.createElement('p');
el.innerText = string;
document.querySelector('#print').appendChild(el);
}
var steps = 1000;
function test(nbody, steps) {
nbody.init();
var start = performance.now();
nbody.bench(steps);
let t = performance.now() - start;
return t;
}
function prologue(name, steps) {
print("Performing " + steps + " steps (" + name + ") ...");
}
function epilogue(time) {
print("\tTook " + (time) + "ms");
}
function sleep(ms) {
return new Promise(r => setTimeout(r, ms));
}
// test code
async function runTests() {
const nbodyJS = JSwrapper(() => { }, {}, _ => _, _ => _);
const nbodyRS = await getRustWASM();
const nbodyRS2 = await getRustWASMWebPack();
const nbodyAS = await getAssemblyScriptWASM();
const nbodyAsmJS = MyAsmModule({});
runCWasm(async nbodyC => {
prologue("C WASM", steps);
epilogue(test(nbodyC, steps));
await sleep(500);
prologue("AssemblyScript WASM", steps);
epilogue(test(nbodyAS, steps));
await sleep(500);
prologue("AssemblyScript ASMJS", steps);
epilogue(test(nbodyAsmJS, steps));
await sleep(500);
prologue("JS", steps);
epilogue(test(nbodyJS, steps));
await sleep(500);
prologue("Rust WASM", steps);
epilogue(test(nbodyRS, steps));
await sleep(500);
prologue("Rust WASM-pack", steps);
epilogue(test(nbodyRS2, steps));
await sleep(500);
print('All done');
});
}
runTests();
</script>
</body>
</html>