Skip to content

Instantly share code, notes, and snippets.

@onliniak
Created February 4, 2024 17:42
Show Gist options
  • Save onliniak/40958aed02c44ab71d72204cde437fbe to your computer and use it in GitHub Desktop.
Save onliniak/40958aed02c44ab71d72204cde437fbe to your computer and use it in GitHub Desktop.
wasm nixos
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
];
}
@onliniak
Copy link
Author

~/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