nx_bounds_perf.nx source
↩ module page · 40 lines · 1230 B
1// nx_bounds_perf.nx -- the COST side of the spatial-safety rung.
2// A hot loop over a fixed array [1024]i64 with a RUNTIME index, which is exactly the shape
3// emit_bounds_check instruments. Compiled by two compilers that differ ONLY in the
4// NX_BOUNDS_CHECK_LIVE const, the delta between the two runs IS the overhead -- no modelling,
5// no estimate. Prints microseconds and the checksum (the checksum also keeps the loop alive
6// against dead-code elimination, and proves both builds computed the SAME answer).
7import "nx_fmt.nx"
8
9const N_ELEM: i64 = 1024
10const N_ITER: i64 = 20000
11
12func main() -> i64 {
13 var a: [1024]i64
14 var i: i64 = 0
15 while i < N_ELEM {
16 a[i] = i * 3 + 1
17 i = i + 1
18 }
19 let t0: i64 = sys_now_us()
20 var acc: i64 = 0
21 var it: i64 = 0
22 while it < N_ITER {
23 var k: i64 = 0
24 while k < N_ELEM {
25 acc = acc + a[k]
26 k = k + 1
27 }
28 it = it + 1
29 }
30 let t1: i64 = sys_now_us()
31 let us: i64 = t1 - t0
32 fmt_puts("BOUNDS-PERF elapsed_us=" as *u8)
33 fmt_putn(us)
34 fmt_puts(" checksum=" as *u8)
35 fmt_putn(acc)
36 fmt_puts(" accesses=" as *u8)
37 fmt_putn(N_ELEM * N_ITER)
38 fmt_puts("\n" as *u8)
39 return 0
40}