code wiki / (root) / nx_slicebench.nx

nx_slicebench.nx source

↩ module page · 36 lines · 1063 B

1// Cost of the SLICE bounds check over heap memory. Same shape and same workload as 2// nx_bounds_perf.nx so the two are directly comparable, and the same shape as the Rust arm in 3// _ops/nxlang_r1/h2h/slice.rs -- Rust's &[T] also carries a length and checks it, which makes 4// that the like-for-like comparison. 5import "nx_fmt.nx" 6const N_ELEM: i64 = 1024 7const N_ITER: i64 = 20000 8func main() -> i64 { 9 let p: *i64 = sys_mmap(8192) as *i64 10 let s: []i64 = __slice(p, N_ELEM) 11 var i: i64 = 0 12 while i < N_ELEM { 13 s[i] = i * 3 + 1 14 i = i + 1 15 } 16 let t0: i64 = sys_now_us() 17 var acc: i64 = 0 18 var it: i64 = 0 19 while it < N_ITER { 20 var k: i64 = 0 21 while k < N_ELEM { 22 acc = acc + s[k] 23 k = k + 1 24 } 25 it = it + 1 26 } 27 let t1: i64 = sys_now_us() 28 fmt_puts("BOUNDS-PERF elapsed_us=" as *u8) 29 fmt_putn(t1 - t0) 30 fmt_puts(" checksum=" as *u8) 31 fmt_putn(acc) 32 fmt_puts(" accesses=" as *u8) 33 fmt_putn(N_ELEM * N_ITER) 34 fmt_puts("\n" as *u8) 35 return 0 36}