DEV Community

Cover image for A word of warning when writing C# benchmarks
G.L Solaria
G.L Solaria

Posted on

4 1

A word of warning when writing C# benchmarks

I wrote a post on the free, open source tool BenchmarkDotNet but I wanted to mention a common pitfall that you might fall in to if you are not careful.

The warning is simple: when writing benchmarks, always use the results of your calculations.

Consider these benchmarks ...

[Benchmark(Baseline = true)]
public void Sqrt1()
{
    for (var ii = 0; ii < N; ++ii)
        Math.Sqrt(ii);
}

[Benchmark]
public double Sqrt2()
{
    double result = 0;
    for (var ii = 0; ii < N; ++ii)
        result = Math.Sqrt(ii);
    return result;
}
Enter fullscreen mode Exit fullscreen mode

If we look at the results of the above benchmarks ...

Method N Mean Error StdDev Ratio RatioSD Rank
Sqrt1 1000000 291.8 us 5.79 us 6.66 us 1.00 0.00 1
Sqrt2 1000000 4,006.9 us 55.00 us 51.45 us 13.64 0.29 2

How can Sqrt2 be almost 14 times slower than Sqrt1? The answer lies with the JIT compiler ...

SharpLab is another great tool that will show you what the JIT compiler will do initially. In the case of Sqrt1, it will compile out dead code because the result of the square root isn't used so it doesn't need to run the instructions to perform the square root.

So be careful with how you write your benchmarks otherwise you may not be measuring what you think you are measuring!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️