-
-
Save onliniak/40958aed02c44ab71d72204cde437fbe to your computer and use it in GitHub Desktop.
run = "crystal run main.cr" | |
# crystal build main.cr --cross-compile --target wasm32-unknown-wasi | |
# wasm2wat main.wasm -o main.wat | |
## wasm2c main.wasm --no-debug-names -o main.c | |
## gcc -O3 -march=native -c main.c -o main.o | |
entrypoint = "main.cr" | |
[nix] | |
channel = "stable-23_11" |
{ pkgs }: { | |
deps = [ | |
pkgs.crystal | |
pkgs.shards | |
pkgs.openssl | |
pkgs.pkg-config | |
pkgs.wabt | |
pkgs.glibc.dev | |
pkgs.gcc | |
pkgs.lld | |
pkgs.llvm | |
pkgs.libllvm | |
pkgs.pcre.dev | |
pkgs.wasmer | |
]; | |
} |
$crystal build --release --no-debug main .cr --cross-compile --target wasm32-unknown-wasi && wasm-ld main.wasm -o main-final.wasm -L wasm32-wasi -lc -lpcre -lclang_rt.builtins-wasm32 && ./wamrc -o main.aot main-final.wasm
$ time ./iwasm main.aot
4
real 0m0.701s
user 0m0.048s
sys 0m0.083s
$ time wasmer run main-final.wasm
4
real 0m0.304s
user 0m0.111s
sys 0m0.054s
~/WetSkilledAngels$ crystal build --release --no-debug main.cr && t
ime ./main
4
real 0m0.008s
user 0m0.001s
sys 0m0.006s
~/WetSkilledAngels$ time ./iwasm main.aot
4
real 0m0.308s
user 0m0.016s
sys 0m0.091s
~/WetSkilledAngels$ time iwasm main.aot
4
real 0m0.118s
user 0m0.025s
sys 0m0.024s
~/WetSkilledAngels$ time iwasm main.aot
4
real 0m0.044s
user 0m0.021s
sys 0m0.022s
~/WetSkilledAngels$ time ./iwasm main.aot
4
real 0m0.081s
user 0m0.026s
sys 0m0.037s
~/WetSkilledAngels$ iwasm --version
iwasm 1.2.3
trait = 10 # Kowalstwo
skill = 5 # Siła
playerlvl = 1
energy = 1
itemlvl = 1
score = Math.hypot(playerlvl, itemlvl) # Im więcej tym gorzej, drugi wyraz jest silniejszy
power = Math.log(skill**trait, score)/10*energy # Pierwszy wyraz w log zwiększa szansę, a drugi ogranicza
difficulty = power/itemlvl
puts difficulty.to_i
W tym przykładzie różnica jest 5.5-10 krotna. 0.008 vs 0.044 vs 0.081
~/WetSkilledAngels$ crystal build --release --no-debug main.cr
~/WetSkilledAngels$ time ./main
"4\n"
real 0m0.118s
user 0m0.013s
sys 0m0.032s
~/WetSkilledAngels$ time iwasm main.aot
4
real 0m0.038s
user 0m0.016s
sys 0m0.022s
stdout = IO::Memory.new
status = Process.run("iwasm", ["main.aot"], output: stdout)
output = stdout.to_s
p output
~/WetSkilledAngels$ time ./main
"4\n"
real 0m0.050s
user 0m0.026s
sys 0m0.014s
~/WetSkilledAngels$
stdout = IO::Memory.new
process = Process.new("iwasm", ["main.aot"], output: stdout)
status = process.wait
p stdout.to_s
~/WetSkilledAngels$ ./main
WASM 35.97 ( 27.80ms) (±38.53%) 37.7kB/op 1956003.75× slower
Native 70.35M ( 14.21ns) (±49.93%) 0.0B/op fastest
require "benchmark"
Benchmark.ips do |x|
x.report("WASM") {
stdout = IO::Memory.new
status = Process.run("iwasm", ["main.aot"], output: stdout)
output = stdout.to_s
}
x.report("Native") {
trait = 10 # Kowalstwo
skill = 5 # Siła
playerlvl = 1
energy = 1
itemlvl = 1
score = Math.hypot(playerlvl, itemlvl) # Im więcej tym gorzej, drugi wyraz jest silniejszy
power = Math.log(skill**trait, score)/10*energy # Pierwszy wyraz w log zwiększa szansę, a drugi ogranicza
difficulty = power/itemlvl
}
end
WAMR 1.3.2
$ wget https://github.com/bytecodealliance/wasm-micro-runtime/releases/download/WAMR-1.3.2/wamrc-1.3.2-x86_64-ubuntu-20.04.tar.gz && tar -xzvf wamrc-1.3.2-x86_64-ubuntu-20.04.tar.gz
$ ./wamrc -o main.aot hello-final.wasm
Create AoT compiler with:
target: x86_64
target cpu: broadwell
target triple: x86_64-unknown-linux-gnu
cpu features:
opt level: 3
size level: 3
output format: AoT file
$ time ./iwasm main.aot
"Test"
real 0m0.095s
user 0m0.014s
sys 0m0.041s
$ time ./main
"Test"
real 0m0.053s
user 0m0.014s
sys 0m0.022s
LLVM Mode:
$ time ./iwasm --llvm-jit hello-final.wasm
"Test"
real 0m29.034s
user 0m12.325s
sys 0m0.639s
$ time crystal run main.cr
"Test"
real 0m16.364s
user 0m5.449s
sys 0m1.551s
Default Crystal build vs default iwasm
$ time ./main
"Test"
real 0m0.071s
user 0m0.017s
sys 0m0.021s
$ time ./iwasm hello-final.wasm
"Test"
real 0m4.716s
user 0m1.997s
sys 0m0.185s
~/WetSkilledAngels$
Tego czegoś nie można traktować jako benchmarka ale nawet tutaj AOT WAMR jest zaledwie ~30% wolniejszy od niezoptymalizowanego kryształa.