Questions tagged [benchmarkdotnet]

BenchmarkDotNet is a powerful .NET library for measuring absolute and relative code performance.

BenchmarkDotNet is open source and available on GitHub under MIT license. It is distributed as a Nuget package.

BenchmarkDotNet executes measured method multiple times and accumulates statistics on time performance of the method, with ability to differentiate a first (warm up) execution time, which might involve JIT comilcation and sustained performance after the code is JIT'ed.

Example of report from BenchmarkDotNet, comparing performance of two alternative algorithms (in this case cryptographic hash sums) -- MD5 and SHA5:

BenchmarkDotNet=v0.9.0.0
OS=Microsoft Windows NT 6.2.9200.0
Processor=Intel(R) Core(TM) i7-4810MQ CPU @ 2.80GHz, ProcessorCount=8
Frequency=2728067 ticks, Resolution=366.5599 ns
HostCLR=MS.NET 4.0.30319.42000, Arch=64-bit RELEASE [RyuJIT]


Type=Md5VsSha256  Mode=Throughput
 Method |      Median |    StdDev
------- |------------ |----------
    Md5 |  21.2912 us | 0.4373 us
 Sha256 | 107.4124 us | 1.8339 us
92 questions
37
votes
2 answers

Running BenchmarkDotNet within XUnit

I am using .NET Core 3.1 in my project (web api, VS2019) and XUnit 2.4.1. Recently I was thinking about adding some performance tests and I came accross this library - BenchmarkDotNet. Since I've already been using XUnit for other tests I wanted to…
esgaldir
  • 823
  • 9
  • 11
14
votes
1 answer

How to read the result table of BenchmarkDotNet

I ran a benchmark example and got this table. BenchmarkDotNet=v0.12.0, OS=Windows 7 SP1 (6.1.7601.0) Intel Xeon CPU E5-4660 v3 2.10GHz, 1 CPU, 28 logical and 14 physical cores Frequency=2050214 Hz, Resolution=487.7540 ns, Timer=TSC [Host] :…
user11847100
12
votes
4 answers

BenchmarkDotNet InProcessEmitToolchain Complete Sample

I'm looking at BenchmarkDotNet and benchmarking in general for the first time ever. I appear to be unable to run benchmarks using the normal BenchmarkRunner because of antivirus restrictions on our work laptops so I'm trying to use…
Thomas Parikka
  • 472
  • 5
  • 17
12
votes
3 answers

How to interpret the results from BenchmarkDotNet and dotMemory?

So, I have a following piece of code in my Main() method for (int x = 0; x < 100; x++) // to mimic BenchmarkDotnet runs for (int y = 0; y < 10000; y++) LogicUnderTest(); Next, I have the following class under the test [MemoryDiagnoser,…
Semuserable
  • 454
  • 6
  • 24
12
votes
2 answers

Show Only Summary Section of BenchmarkDotNet

I'm benchmarking some .net framework stuffs, I'm using .net framework, C# and BenchmarkDotNet What I want to do is; I'm writing a lot of benchmark tests and I'm only interested in summary sections of the reports. How can I configure BenchmarkDotNet…
Lost_In_Library
  • 3,265
  • 6
  • 38
  • 70
11
votes
1 answer

Is it possible to use Benchmark.NET to "fail" a CI build if performance has regressed too much?

I have unit tests. If one of them fails, my build fails. I would like to apply the same principle to performance. I have a series of microbenchmarks for several hot paths through a library. Empirically, slowdowns in these areas have a…
rianjs
  • 7,767
  • 5
  • 24
  • 40
9
votes
1 answer

What can explain the overhead of using const in this case?

I'm banging my head against the wall here, so I hope that some of you may be able to educate me. I was doing some performance benchmarks using BenchmarkDotNet and I ran into this weird case where it seems that declaring a member const degrades…
Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
9
votes
1 answer

Performance difference between C# for-loop and Array.Fill

I have implemented the following benchmark using BenchmarkDotNet: public class ForVsFillVsEnumerable { private bool[] data; [Params(10, 100, 1000)] public int N; [GlobalSetup] public void Setup() { data = new…
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
8
votes
1 answer

Why does Mono run a simple method slower whereas RyuJIT runs it significantly faster?

I created a simple benchmark out of curiosity, but cannot explain the results. As benchmark data, I prepared an array of structs with some random values. The preparation phase is not benchmarked: struct Val { public float val; public float…
dymanoid
  • 14,771
  • 4
  • 36
  • 64
8
votes
1 answer

BenchmarkDotNet with async task

I'm trying to run this code : public class Parsing { private const string Url ="blabla"; private static HttpClient client = new HttpClient(); private static Task newton = ParseNewton(); private static Task
Cholesterol
  • 177
  • 2
  • 10
7
votes
1 answer

BenchmarkDotNet - How to inject parameters outside from the class

I'm using BenchmarkDotNet library for performance checks and I want to inject parameters when using the benchmark class. Something like this: public class Program { public static void Main() { var benchmark1 = new…
itaiy
  • 1,152
  • 1
  • 13
  • 22
7
votes
1 answer

Benchmarking Newtonsoft.Json deserialization: from stream and from string

I'm interested in performance (speed, memory usage) comparison of two approaches how to deserialize HTTP response JSON payload using Newtonsoft.Json. I'm aware of Newtonsoft.Json's Performance Tips to use streams, but I wanted to know more and have…
Zdeněk
  • 929
  • 1
  • 8
  • 25
7
votes
1 answer

Is Activating a Struct Without Storing It as a Local Variable Expected to Be Slower than Not Storing It as a Local Variable?

I have encountered a performance issue in .NET Core 2.1 that I am trying to understand. The code for this can be found here: https://github.com/mike-eee/StructureActivation Here is the relavant benchmark code via BenchmarkDotNet: public class…
Mike-E
  • 2,477
  • 3
  • 22
  • 34
5
votes
2 answers

Is it possible to use #if NET6_0_OR_GREATER to exclude a benchmark method from a BenchmarkDotNet run?

Suppose that you're writing some benchmarks for use with BenchmarkDotNet that are multi-targeted to net48 and net6.0, and that one of those benchmarks can only be compiled for the net6.0 target. The obvious thing to do is to use something like this…
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
5
votes
3 answers

Why is more memory allocated than expected?

I'm playing around with BenchmarkDotNet and its MemoryDiagnoser feature. Considering the following benchmark: [Benchmark] public void Dummy() { var buffer = new byte[1]; } I expect it to allocate exactly 1 byte. But the benchmark result shows…
silkfire
  • 24,585
  • 15
  • 82
  • 105
1
2 3 4 5 6 7