/
index.html
80 lines (57 loc) · 1.92 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
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="module">
import Thread from '../Thread.js';
// Operation functions
const interleave = (a, b, res) => {
let i = 0;
while (true) {
if (i >= a.length || i >= b.length) break;
res[2 * i + 0] = a[i];
res[2 * i + 1] = b[i];
i++;
}
return res;
};
const threadFunc = args => {
const arr1 = args.arr1;
const arr2 = args.arr2;
const res = args.res;
postMessage('starting');
const data = interleave(arr1, arr2, res);
postMessage('done');
return data;
};
// Create thread
const thread = new Thread(threadFunc, { interleave });
// Create data
const ARRAY_LENGTH = 100000000;
const arr1 = new Float32Array(new SharedArrayBuffer(ARRAY_LENGTH * 4));
const arr2 = new Float32Array(new SharedArrayBuffer(ARRAY_LENGTH * 4));
const sharedres = new Float32Array(new SharedArrayBuffer(ARRAY_LENGTH * 4 * 2));
for (let i = 0; i < ARRAY_LENGTH; i++) {
arr1[i] = Math.random();
arr2[i] = Math.random();
}
// Run the tests
console.time('main thread run');
interleave(arr1, arr2, sharedres);
console.timeEnd('main thread run');
console.time('initial thread run');
thread
.run({ arr1, arr2, res: sharedres }, log => console.log(log))
.then(res => {
console.timeEnd('initial thread run');
console.time('subsequent thread run');
return thread.run({ arr1, arr2, res: sharedres }, log => console.log(log));
})
.then(res => {
console.timeEnd('subsequent thread run');
});
</script>
</head>
<body>
</body>
</html>