WebAssembly and Emscripten
Sebastiano Tronto
ALTEN Advanced Software Evening
var aInput = document.getElementById("aInput");
var bInput = document.getElementById("bInput");
var button = document.getElementById("resultButton");
var resultText = document.getElementById("resultText");
button.addEventListener("click", () => {
var a = Number(aInput.value);
var b = Number(bInput.value);
resultText.innerText = a + b;
});
(func (param i64) (result i64)
local.get 0
i64.eqz
if (result i64)
i64.const 1
else
local.get 0
local.get 0
i64.const 1
i64.sub
call 0
i64.mul
end)
(func (param i64) (result i64)
local.get 0
i64.eqz
if (result i64)
i64.const 1
else
local.get 0
local.get 0
i64.const 1
i64.sub
call 0
i64.mul
end)
def f(n):
if n == 0:
return 1
else
return n * f(n-1)
"Emscripten is a complete compiler toolchain to WebAssembly, using LLVM, with a special focus on speed, size, and the Web platform."
emcc -o index.html hello.c
emcc -sEXPORTED_FUNCTIONS=_sum \
-sMODULARIZE \
-sEXPORT_NAME=SumLibrary \
-o sum_library.mjs \
sum_library.c
| Firefox WASM (-O3) | Firefox JS |
Chromium WASM (-O3) | Chromium JS |
|
|---|---|---|---|---|
| 1e7 Primes (iterative) | 4.5s | 4.4s | 4.7s | 4.6s |
| 1e7 Primes (sieve) | 0.76s | 0.82s | 0.92s | 0.80s |
| Matrix multiplication | 0.81s | 5.3s | 1.5s | 4.6s |
See github.com/sebastianotronto/emscripten-tutorial
Drinks!