Created
February 4, 2024 17:42
-
-
Save onliniak/40958aed02c44ab71d72204cde437fbe to your computer and use it in GitHub Desktop.
wasm nixos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ 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 | |
]; | |
} |
~/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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$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
W tym przykładzie różnica jest 5.5-10 krotna. 0.008 vs 0.044 vs 0.081