-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
70 lines (56 loc) · 1.72 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WASM TEST</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<script>
async function load() {
let c_binary = await fetch('/c/fib.wasm');
let cpp_binary = await fetch('/cpp/fib.wasm');
let c_bytes = await c_binary.arrayBuffer();
let cpp_bytes = await cpp_binary.arrayBuffer();
let c_module = await WebAssembly.compile(c_bytes);
let cpp_module = await WebAssembly.compile(cpp_bytes);
let c_instance = await WebAssembly.Instance(c_module, {});
let cpp_instance = await WebAssembly.Instance(cpp_module, {});
return [c_instance, cpp_instance];
}
let funcs = [];
load()
.then(
instances => {
instances.map(
instance => {
funcs.push(instance.exports);
}
);
exec(40);
}
);
function exec(times) {
let begin = performance.now();
console.log(funcs[0].fib(times));
console.log("C WASM takes: " + (performance.now() - begin).toString() );
begin = performance.now();
console.log(funcs[1]._Z3fibi(times));
console.log("C++ WASM takes: " + (performance.now() - begin).toString() );
begin = performance.now();
console.log(fib(times));
console.log("Vanilla JS takes: " + (performance.now() - begin).toString() );
}
function fib(n) {
if(n<0)
return -1;
else if (n==0)
return 0;
else if (n==1)
return 1;
else if (n>1)
return fib(n-1) + fib(n-2);
}
</script>
</body>
</html>