|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using BenchmarkDotNet.Attributes; |
| 4 | + |
| 5 | +namespace StructLinq.Benchmark |
| 6 | +{ |
| 7 | + //``` ini |
| 8 | + |
| 9 | + //BenchmarkDotNet=v0.12.0, OS=Windows 10.0.19041 |
| 10 | + //Intel Core i7-7700 CPU 3.60GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores |
| 11 | + //.NET Core SDK=3.1.402 |
| 12 | + //[Host] : .NET Core 3.1.8 (CoreCLR 4.700.20.41105, CoreFX 4.700.20.41903), X64 RyuJIT |
| 13 | + //DefaultJob : .NET Core 3.1.8 (CoreCLR 4.700.20.41105, CoreFX 4.700.20.41903), X64 RyuJIT |
| 14 | + |
| 15 | + |
| 16 | + //``` |
| 17 | + //| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated | |
| 18 | + //|----------------------------------------- |----------:|---------:|---------:|------:|--------:|------:|------:|------:|----------:| |
| 19 | + //| Linq | 110.22 us | 2.488 us | 6.976 us | 1.00 | 0.00 | - | - | - | 80 B | |
| 20 | + //| Array | 104.70 us | 2.050 us | 3.481 us | 0.94 | 0.08 | - | - | - | 80 B | |
| 21 | + //| StructLinq | 43.85 us | 0.550 us | 0.459 us | 0.37 | 0.03 | - | - | - | - | |
| 22 | + //| RefStructLinq | 51.53 us | 1.077 us | 1.058 us | 0.44 | 0.03 | - | - | - | - | |
| 23 | + //| StructLinqWithCustomComparer | 35.22 us | 0.636 us | 0.563 us | 0.30 | 0.02 | - | - | - | - | |
| 24 | + //| RefStructLinqZeroAllocwithCustomComparer | 21.92 us | 0.160 us | 0.149 us | 0.19 | 0.01 | - | - | - | - | |
| 25 | + |
| 26 | + |
| 27 | + [MemoryDiagnoser] |
| 28 | + public class ContainsWhereOnBigStruct |
| 29 | + { |
| 30 | + private const int Count = 10_000; |
| 31 | + private readonly StructContainer[] array; |
| 32 | + private readonly IEnumerable<StructContainer> enumerable; |
| 33 | + |
| 34 | + public ContainsWhereOnBigStruct() |
| 35 | + { |
| 36 | + array = Enumerable.Range(0, Count).Select(StructContainer.Create).ToArray(); |
| 37 | + enumerable = Enumerable.Range(0, Count).Select(StructContainer.Create).ToArray(); |
| 38 | + } |
| 39 | + |
| 40 | + [Benchmark(Baseline = true)] |
| 41 | + public bool Linq() |
| 42 | + { |
| 43 | + return enumerable.Where(x=> true).Contains(StructContainer.Create(5_000)); |
| 44 | + } |
| 45 | + |
| 46 | + [Benchmark] |
| 47 | + public bool Array() |
| 48 | + { |
| 49 | + return array.Where(x=> true).Contains(StructContainer.Create(5_000)); |
| 50 | + } |
| 51 | + |
| 52 | + [Benchmark] |
| 53 | + public bool StructLinq() |
| 54 | + { |
| 55 | + return array.ToStructEnumerable().Where(x=> true, x=>x).Contains(StructContainer.Create(5_000), x=>x); |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + [Benchmark] |
| 60 | + public bool RefStructLinq() |
| 61 | + { |
| 62 | + return array.ToRefStructEnumerable().Where((in StructContainer x)=> true, x=>x).Contains(StructContainer.Create(5_000), x=> x); |
| 63 | + } |
| 64 | + |
| 65 | + [Benchmark] |
| 66 | + public bool StructLinqWithCustomComparer() |
| 67 | + { |
| 68 | + var comparer = new DistinctOnBigStruct.StructEqualityComparer(); |
| 69 | + return array.ToStructEnumerable().Where(x=> true, x=>x).Contains(StructContainer.Create(5_000), comparer, x=>x); |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + [Benchmark] |
| 74 | + public bool RefStructLinqZeroAllocwithCustomComparer() |
| 75 | + { |
| 76 | + var comparer = new DistinctOnBigStruct.StructEqualityComparer(); |
| 77 | + return array.ToRefStructEnumerable().Where((in StructContainer x)=> true, x=>x).Contains(StructContainer.Create(5_000), comparer, x=> x); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments