Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active October 31, 2021 14:53
Show Gist options
  • Save guitarrapc/e400d5bc932b5368e28113829895c189 to your computer and use it in GitHub Desktop.
Save guitarrapc/e400d5bc932b5368e28113829895c189 to your computer and use it in GitHub Desktop.
BenchmarkRunner.Run<BenchmarkLinqWhereFirst>();
[BenchmarkDotNet.Attributes.MemoryDiagnoser]
public class BenchmarkLinqWhereFirst
{
private readonly int[] data;
public BenchmarkLinqWhereFirst()
{
data = Enumerable.Range(1, 1000).ToArray();
}
[Benchmark]
public int WhereFirst() => data.Where(x => x > 100).First();
[Benchmark]
public void First() => data.First(x => x > 100);
}
// Validating benchmarks:
Assembly LINQPadQuery, Version=1.0.0.213, Culture=neutral, PublicKeyToken=null is located in temp. If you are running benchmarks from xUnit you need to disable shadow copy. It's not supported by design.
// ***** BenchmarkRunner: Start *****
// * Summary *
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
AMD Ryzen 9 5900X, 1 CPU, 24 logical and 12 physical cores
.NET SDK=6.0.100-rc.2.21505.57
[Host] : .NET 5.0.11 (5.0.1121.47308), X64 RyuJIT
| Method | Mean | Error | StdDev | Gen 0 | Allocated |
|----------- |---------:|--------:|--------:|-------:|----------:|
| WhereFirst | 166.9 ns | 0.52 ns | 0.48 ns | 0.0029 | 48 B |
| First | 404.8 ns | 1.09 ns | 0.91 ns | 0.0019 | 32 B |
// * Legends *
Mean : Arithmetic mean of all measurements
Error : Half of 99.9% confidence interval
StdDev : Standard deviation of all measurements
Gen 0 : GC Generation 0 collects per 1000 operations
Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)
1 ns : 1 Nanosecond (0.000000001 sec)
// * Diagnostic Output - MemoryDiagnoser *
// ***** BenchmarkRunner: End *****
// ** Remained 0 benchmark(s) to run **
Run time: 00:00:38 (38.6 sec), executed benchmarks: 2
Global total time: 00:00:38 (38.64 sec), executed benchmarks: 2
// * Artifacts cleanup *
// Validating benchmarks:
Assembly LINQPadQuery, Version=1.0.0.4, Culture=neutral, PublicKeyToken=null is located in temp. If you are running benchmarks from xUnit you need to disable shadow copy. It's not supported by design.
// ***** BenchmarkRunner: Start *****
// * Summary *
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
AMD Ryzen 9 5900X, 1 CPU, 24 logical and 12 physical cores
.NET SDK=6.0.100-rc.2.21505.57
[Host] : .NET 6.0.0 (6.0.21.48005), X64 RyuJIT
| Method | Mean | Error | StdDev | Gen 0 | Allocated |
|----------- |---------:|--------:|--------:|-------:|----------:|
| WhereFirst | 182.3 ns | 1.60 ns | 1.42 ns | 0.0029 | 48 B |
| First | 444.8 ns | 3.26 ns | 2.73 ns | 0.0019 | 32 B |
// * Legends *
Mean : Arithmetic mean of all measurements
Error : Half of 99.9% confidence interval
StdDev : Standard deviation of all measurements
Gen 0 : GC Generation 0 collects per 1000 operations
Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)
1 ns : 1 Nanosecond (0.000000001 sec)
// * Diagnostic Output - MemoryDiagnoser *
// ***** BenchmarkRunner: End *****
// ** Remained 0 benchmark(s) to run **
Run time: 00:00:43 (43.05 sec), executed benchmarks: 2
Global total time: 00:00:43 (43.06 sec), executed benchmarks: 2
// * Artifacts cleanup *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment