First Commit
This commit is contained in:
59
wasm_pthread_fast/web/index.html
Normal file
59
wasm_pthread_fast/web/index.html
Normal file
@@ -0,0 +1,59 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>WASM Pthreads Demo</title>
|
||||
<script type="module">
|
||||
import WasmModule from './wasm_add_pthread.js';
|
||||
|
||||
async function main() {
|
||||
const loadStart = performance.now();
|
||||
const wasmModule = await WasmModule(); // fully initialized
|
||||
const loadDuration = performance.now() - loadStart;
|
||||
console.log(`WASM module load time: ${loadDuration} ms`);
|
||||
|
||||
if (!wasmModule.HEAPU8 || !wasmModule.HEAPU8.buffer) {
|
||||
console.error("HEAPU8 or HEAPU8.buffer not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
const ARRAY_SIZE = 15000576;
|
||||
const allocStart = performance.now();
|
||||
const aPtr = wasmModule._alloc_float_array(ARRAY_SIZE);
|
||||
const bPtr = wasmModule._alloc_float_array(ARRAY_SIZE);
|
||||
const cPtr = wasmModule._alloc_float_array(ARRAY_SIZE);
|
||||
const allocDuration = performance.now() - allocStart;
|
||||
console.log(`Memory allocation time: ${allocDuration} ms`);
|
||||
|
||||
const initStart = performance.now();
|
||||
wasmModule._init_arrays(aPtr, bPtr);
|
||||
const initDuration = performance.now() - initStart;
|
||||
console.log(`Array initialization time: ${initDuration} ms`);
|
||||
|
||||
// Create a Float32Array view on the WASM memory buffer
|
||||
const memory = new Float32Array(wasmModule.HEAPU8.buffer);
|
||||
|
||||
const calcStart = performance.now();
|
||||
for (let i = 0; i < 100; i++) {
|
||||
wasmModule._calculate_range(aPtr, bPtr, cPtr, 0, ARRAY_SIZE);
|
||||
}
|
||||
const calcDuration = performance.now() - calcStart;
|
||||
console.log(`Average calc time: ${(calcDuration / 100).toFixed(2)} ms`);
|
||||
|
||||
for (let i = 0; i < 10; i++) {
|
||||
console.log(`c[${i}] = ${memory[cPtr / 4 + i]}`);
|
||||
}
|
||||
|
||||
const deallocStart = performance.now();
|
||||
wasmModule._dealloc_float_array(aPtr);
|
||||
wasmModule._dealloc_float_array(bPtr);
|
||||
wasmModule._dealloc_float_array(cPtr);
|
||||
const deallocDuration = performance.now() - deallocStart;
|
||||
console.log(`Memory deallocation time: ${deallocDuration} ms`);
|
||||
}
|
||||
|
||||
main();
|
||||
</script>
|
||||
</head>
|
||||
<body></body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user