Skip to content
All guides

systems

Performance

Gate B is equivalent-n loops: Buraaq --release versus C++ -O2, clang 22. The same n is in both sources. Unfavorable numbers stay published.

Results

BenchnC++ -O2BuraaqRatio
nested_loop1e4²0.010s0.002s0.16×
integer_sum1e80.015s<0.001s0.00×
float_saxpy1e70.004s0.004s0.97×
numerical_loop1e80.083s0.084s1.01×
fib_iter1e80.020s0.021s1.02×

Four benches beat or match C++. integer_sum is an Orbit fold of wrapping sum = sum * 3 + i (same n=1e8; n=10 checksum 44281). Worst published: fib_iter 1.02×. n was not reduced.

What we measure

  • Timed region is only the loop. Setup and print sit outside the clock.
  • n is kept alive (volatile in C++, buraaq_keepalive_i32 in Buraaq) so the optimizer cannot delete the work.
  • Buraaq signed add / sub emit LLVM nsw, the same flag clang uses for C++ signed math.
  • for i in 1..=n parses when n is an identifier (the end of a range is not a struct literal).

How to write a hot loop

Write the obvious while or for. Measure --release before you clever it. --release-fast is -O3 plus thin LTO when you care about the last percent.

fn main() {
    n = 100000000
    mut sum = 0
    mut i = 1
    while i <= n {
        sum = sum * 3 + i
        i = i + 1
    }
    println(sum)
}

Do not drop bounds checks or reach for unsafe to “win” a bench. Gate B was taken in safe Buraaq.

Next: electronics if the loop sits on a board.