|
| 1 | +// LinqGen.Benchmarks, Maxwell Keonwoo Kang <code.athei@gmail.com>, 2022 |
| 2 | + |
| 3 | +using BenchmarkDotNet.Attributes; |
| 4 | +using Cathei.LinqGen; |
| 5 | +using Cathei.LinqGen.Hidden; |
| 6 | +using StructLinq; |
| 7 | +using NetFabric.Hyperlinq; |
| 8 | +using StructLinq.Range; |
| 9 | + |
| 10 | +namespace Cathei.LinqGen.Benchmarks.Cases; |
| 11 | + |
| 12 | +[MemoryDiagnoser] |
| 13 | +public class ListVsPooledList |
| 14 | +{ |
| 15 | + private int[] TestData { get; set; } = null!; |
| 16 | + |
| 17 | + [Params(100, 10_000, 1_000_000)] |
| 18 | + public int Count { get; set; } |
| 19 | + |
| 20 | + private List<int> sharedList; |
| 21 | + private PooledListNative<int> sharedNative; |
| 22 | + private PooledListManaged<int> sharedManaged; |
| 23 | + |
| 24 | + [GlobalSetup] |
| 25 | + public void GlobalSetup() |
| 26 | + { |
| 27 | + TestData = Utils.Generate(42, Count).ToArray(); |
| 28 | + |
| 29 | + sharedList = new List<int>(Count); |
| 30 | + sharedNative = new PooledListNative<int>(Count); |
| 31 | + sharedManaged = new PooledListManaged<int>(Count); |
| 32 | + } |
| 33 | + |
| 34 | + [GlobalCleanup] |
| 35 | + public void GlobalCleanup() |
| 36 | + { |
| 37 | + sharedNative.Dispose(); |
| 38 | + sharedManaged.Dispose(); |
| 39 | + } |
| 40 | + |
| 41 | + [Benchmark(Baseline = true)] |
| 42 | + public List<int> List() |
| 43 | + { |
| 44 | + var buffer = new List<int>(TestData.Length); |
| 45 | + // var buffer = sharedList; |
| 46 | + |
| 47 | + buffer.Clear(); |
| 48 | + |
| 49 | + for (int i = 0; i < TestData.Length; ++i) |
| 50 | + { |
| 51 | + buffer.Add(TestData[i] * 2); |
| 52 | + } |
| 53 | + |
| 54 | + return buffer; |
| 55 | + } |
| 56 | + |
| 57 | + [Benchmark()] |
| 58 | + public PooledListNative<int> Native() |
| 59 | + { |
| 60 | + using var buffer = new PooledListNative<int>(TestData.Length); |
| 61 | + |
| 62 | + // var buffer = sharedNative; |
| 63 | + |
| 64 | + buffer.Clear(); |
| 65 | + |
| 66 | + for (int i = 0; i < TestData.Length; ++i) |
| 67 | + { |
| 68 | + buffer.Add(TestData[i] * 2); |
| 69 | + } |
| 70 | + |
| 71 | + return buffer; |
| 72 | + } |
| 73 | + |
| 74 | + [Benchmark()] |
| 75 | + public PooledListManaged<int> Managed() |
| 76 | + { |
| 77 | + using var buffer = new PooledListManaged<int>(TestData.Length); |
| 78 | + |
| 79 | + // var buffer = sharedManaged; |
| 80 | + |
| 81 | + buffer.Clear(); |
| 82 | + |
| 83 | + for (int i = 0; i < TestData.Length; ++i) |
| 84 | + { |
| 85 | + buffer.Add(TestData[i] * 2); |
| 86 | + } |
| 87 | + |
| 88 | + return buffer; |
| 89 | + } |
| 90 | + |
| 91 | + // [Benchmark(Baseline = true)] |
| 92 | + // public int[] Linq() |
| 93 | + // { |
| 94 | + // return TestData |
| 95 | + // .Select(x=> x * 2) |
| 96 | + // .ToArray(); |
| 97 | + // } |
| 98 | + // |
| 99 | + // [Benchmark] |
| 100 | + // public int[] LinqGenDelegate() |
| 101 | + // { |
| 102 | + // return TestData |
| 103 | + // .Specialize() |
| 104 | + // .Select(x => x * 2) |
| 105 | + // .ToArray(); |
| 106 | + // } |
| 107 | + // |
| 108 | + // [Benchmark] |
| 109 | + // public int[] LinqGenStruct() |
| 110 | + // { |
| 111 | + // return TestData |
| 112 | + // .Specialize() |
| 113 | + // .Select(new Selector()) |
| 114 | + // .ToArray(); |
| 115 | + // } |
| 116 | + // |
| 117 | + // [Benchmark] |
| 118 | + // public int[] StructLinqDelegate() |
| 119 | + // { |
| 120 | + // return TestData |
| 121 | + // .ToStructEnumerable() |
| 122 | + // .Select(x => x * 2) |
| 123 | + // .ToArray(); |
| 124 | + // } |
| 125 | + // |
| 126 | + // [Benchmark] |
| 127 | + // public int[] StructLinqStruct() |
| 128 | + // { |
| 129 | + // var selector = new Selector(); |
| 130 | + // |
| 131 | + // return TestData |
| 132 | + // .ToStructEnumerable() |
| 133 | + // .Select(ref selector, x => x, x => x) |
| 134 | + // .ToArray(x => x); |
| 135 | + // } |
| 136 | + // |
| 137 | + // [Benchmark] |
| 138 | + // public int[] HyperLinqDelegate() |
| 139 | + // { |
| 140 | + // return TestData |
| 141 | + // .AsValueEnumerable() |
| 142 | + // .Select(x => x * 2) |
| 143 | + // .ToArray(); |
| 144 | + // } |
| 145 | + // |
| 146 | + // [Benchmark] |
| 147 | + // public int[] HyperLinqStruct() |
| 148 | + // { |
| 149 | + // return TestData |
| 150 | + // .AsValueEnumerable() |
| 151 | + // .Select<int, Selector>() |
| 152 | + // .ToArray(); |
| 153 | + // } |
| 154 | + |
| 155 | + readonly struct Selector : |
| 156 | + StructLinq.IFunction<int, int>, |
| 157 | + NetFabric.Hyperlinq.IFunction<int, int>, |
| 158 | + IStructFunction<int, int> |
| 159 | + { |
| 160 | + public int Eval(int element) |
| 161 | + { |
| 162 | + return element * 2; |
| 163 | + } |
| 164 | + |
| 165 | + public int Invoke(int arg) |
| 166 | + { |
| 167 | + return arg * 2; |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments