slovo/benchmarks/vec-string-eq-loop/src/main.slo
2026-05-22 08:38:43 +02:00

68 lines
1.9 KiB
Plaintext

; Benchmark scaffold fixture for local-machine vec-string-equality timing comparisons only.
; Keep LOOP_COUNT and EXPECTED_CHECKSUM aligned with the C/Rust/Python fixtures.
; The runner supplies the target through argv and the loop count through stdin
; or argv so the equality path stays runtime-configured.
(module main)
(import std.vec_string (empty append2 append3 at))
(fn loop_count () -> i32
1000000)
(fn expected_checksum () -> i32
4600001)
(fn parse_stdin_loop_count () -> (result i32 i32)
(let input (result string i32) (std.io.read_stdin_result))
(match input
((ok text)
(std.string.parse_i32_result text))
((err code)
(err i32 i32 code))))
(fn parse_arg_loop_count () -> (result i32 i32)
(std.string.parse_i32_result (std.process.arg 2)))
(fn configured_stdin_loop_count () -> i32
(let parsed_stdin (result i32 i32) (parse_stdin_loop_count))
(if (is_ok parsed_stdin)
(unwrap_ok parsed_stdin)
(loop_count)))
(fn configured_loop_count () -> i32
(let parsed_arg (result i32 i32) (parse_arg_loop_count))
(if (is_ok parsed_arg)
(unwrap_ok parsed_arg)
(configured_stdin_loop_count)))
(fn target_text () -> string
(std.process.arg 1))
(fn words () -> (vec string)
(append2 (append3 (empty) "alpha" "omega" "delta") "omega" "sigma"))
(fn vec_string_eq_loop ((limit i32) (target string)) -> i32
(let values (vec string) (words))
(var i i32 0)
(var acc i32 1)
(while (< i limit)
(set acc (if (= (at values (% i 5)) target)
(+ acc 7)
(+ acc 3)))
(set acc (if (> acc 1000000000)
(- acc 1000000000)
acc))
(set i (+ i 1)))
acc)
(fn main () -> i32
(let result i32 (vec_string_eq_loop (configured_loop_count) (target_text)))
(std.io.print_i32 result)
(if (= result (expected_checksum))
0
1))
(test "vec string equality loop checksum is deterministic"
(= (vec_string_eq_loop (loop_count) "omega") (expected_checksum)))