dotnet / BenchmarkDotNet

Powerful .NET library for benchmarking
https://benchmarkdotnet.org
MIT License
10.24k stars 952 forks source link

How can I obtain the addition result of two int variables without using a loop for testing? #2576

Closed ooosan closed 1 month ago

ooosan commented 1 month ago
    [Benchmark]
    public void IntAddition()
    {
        int a = 10;
        int b = 20;
        int c = a + b;
    }

The running result displays an average of 0.0043ns, which is too small.

timcassell commented 1 month ago

Currently not possible. See #1133 and #1802.

Also, the code you pasted is likely constant-optimized by the JIT, so a zero result would be expected. You would need to use arguments or fields to prevent constant-optimizations.