I was curious, so I ran your zig version myself and ported it to rust[1].
I think you forgot to run your benchmark in release mode. In debug mode, I get similar results to you. But in release mode, it runs ~5x faster than you reported:
genericWriter - 4035.47ns per iterations
appendSlice - 4026.41ns per iterations
appendSliceOptimized - 2884.84ns per iterations
I bet the first two implementations are emitting identical code. But appendSliceOptimized is clearly much more efficient.
For some reason, rust is about twice as fast as zig in this test:
I think you forgot to run your benchmark in release mode. In debug mode, I get similar results to you. But in release mode, it runs ~5x faster than you reported:
I bet the first two implementations are emitting identical code. But appendSliceOptimized is clearly much more efficient.For some reason, rust is about twice as fast as zig in this test:
(1µs = 1000ns)[1] https://gist.github.com/josephg/e2dd6e7baf0a764a21bd724f8a2e...
Tests running on linux 6.8.0, CPU: Ryzen 7950x. zig v0.13 / rust v1.84.1.
Zig command: zig build --release=fast -Dtarget=native run
Rust command: RUSTFLAGS='-C target-cpu=native' cargo run --release -- --bench