r/dotnet • u/VladislavPrimakov • 21h ago
Benchmarking C# 15 [Union] Result, TryOut, and throw/catch (.NET 11 NativeAOT, with & without runtime-async) vs Rust
I benchmarked error-handling patterns in .NET 11 NativeAOT (C# 15 [Union] Result<int, ErrorCode>, classic TryOut, and throw/catch) against Rust (Result<i32, DomainError> and panic::catch_unwind), and also tested the impact of .NET 11's <Features>runtime-async=on</Features> on async paths. Tooling: hyperfine v2 (with hardware perf counters), .NET 11.0.100 NativeAOT (IlcInstructionSet=native, OptimizationPreference=Speed), Rust 1.100.0 (opt-level=3, lto=true, codegen-units=1, target-cpu=native, panic="unwind")
Sync (300M operations, 10% failures)
| Variant | Wall [s] | ns/op | Peak RAM [MiB] | Instructions | IPC | Cache Miss | Branch Misses | Relative |
|---|---|---|---|---|---|---|---|---|
Rust Result | 0.496 ± 0.033 | 1.65 | 1.7 | 7.23B | 3.19 | 83.2K (33.5%) | 335.1K | 1.00x |
C# TryOut | 0.525 ± 0.018 | 1.75 | 3.5 | 8.73B | 3.67 | 93.9K (35.3%) | 4.1M | 1.06x |
C# Result ([Union]) | 1.497 ± 0.015 | 4.99 | 3.5 | 12.39B | 1.77 | 111.0K (29.6%) | 4.2M | 3.02x |
C# Throw | 27.905 ± 0.146 | 93.02 | 54.4 | 414.15B | 3.21 | 47.3M (11.8%) | 44.5M | 56.21x |
Rust Panic (catch_unwind) | 40.665 ± 0.214 | 135.55 | 1.8 | 657.66B | 3.46 | 16.5M (8.5%) | 48.6M | 81.91x |
Async (80M operations, 10% failures)
| Variant | Wall [s] | ns/op | Peak RAM [MiB] | Instructions | IPC | Cache Miss | Branch Misses | Relative |
|---|---|---|---|---|---|---|---|---|
Rust AsyncResult | 0.475 ± 0.002 | 5.94 | 1.8 | 3.05B | 1.37 | 34.4K (28.4%) | 388.3K | 1.00x |
C# AsyncResult (runtime-async) | 0.512 ± 0.003 | 6.40 | 3.5 | 6.27B | 2.70 | 83.7K (35.3%) | 4.9M | 1.08x |
C# AsyncResult (no runtime-async) | 2.111 ± 0.022 | 26.39 | 3.5 | 17.08B | 1.75 | 435.9K (35.5%) | 7.8M | 4.44x |
C# AsyncThrow (runtime-async) | 8.643 ± 0.066 | 108.04 | 51.8 | 131.34B | 3.30 | 14.4M (12.5%) | 12.8M | 18.21x |
C# AsyncThrow (no runtime-async) | 22.745 ± 0.032 | 284.31 | 52.4 | 325.94B | 3.09 | 69.7M (6.3%) | 89.7M | 47.84x |
https://github.com/VladislavPrimakov/benchmarks#7-throw-vs-result-benchmark-c-nativeaot-vs-rust

