|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using BenchmarkDotNet.Columns; |
| 3 | +using BenchmarkDotNet.Configs; |
| 4 | +using BenchmarkDotNet.Diagnosers; |
| 5 | +using BenchmarkDotNet.Jobs; |
| 6 | +using BenchmarkDotNet.Order; |
| 7 | +using Microsoft.Extensions.Primitives; |
| 8 | +using Open.Text; |
| 9 | +using System.Text.RegularExpressions; |
| 10 | +using ZLinq; |
| 11 | + |
| 12 | +namespace Open.Text.Benchmarks; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Comprehensive benchmarks measuring the ZLinq integration improvements. |
| 16 | +/// Compares allocation behavior across different operations. |
| 17 | +/// </summary> |
| 18 | +[Config(typeof(Config))] |
| 19 | +[MemoryDiagnoser] |
| 20 | +[Orderer(SummaryOrderPolicy.FastestToSlowest)] |
| 21 | +[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] |
| 22 | +[CategoriesColumn] |
| 23 | +public class ZLinqImprovementsBenchmark |
| 24 | +{ |
| 25 | + private class Config : ManualConfig |
| 26 | + { |
| 27 | + public Config() |
| 28 | + { |
| 29 | + AddDiagnoser(MemoryDiagnoser.Default); |
| 30 | + AddColumn(StatisticColumn.Mean); |
| 31 | + AddColumn(StatisticColumn.Median); |
| 32 | + AddColumn(RankColumn.Arabic); |
| 33 | + AddJob(Job.ShortRun.WithId("ZLinq")); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // Test data |
| 38 | + private const string SmallCsv = "apple,banana,cherry,date,elderberry"; |
| 39 | + private const string MediumCsv = "apple,banana,cherry,date,elderberry,fig,grape,honeydew,kiwi,lemon,mango,nectarine,orange,papaya,quince"; |
| 40 | + private static readonly string LargeCsv = string.Join(",", Enumerable.Range(1, 1000).Select(i => $"item{i}")); |
| 41 | + |
| 42 | + private static readonly Regex CommaRegex = new(",", RegexOptions.Compiled); |
| 43 | + private static readonly Regex WordRegex = new(@"\w+", RegexOptions.Compiled); |
| 44 | + |
| 45 | + // ===================================================================== |
| 46 | + // CATEGORY: Char Split - Foreach Only (Pure enumeration, no materialization) |
| 47 | + // ===================================================================== |
| 48 | + |
| 49 | + [BenchmarkCategory("CharSplit-Foreach"), Benchmark(Baseline = true, Description = "BCL String.Split")] |
| 50 | + public int CharSplit_Foreach_BCL() |
| 51 | + { |
| 52 | + int count = 0; |
| 53 | + foreach (var s in SmallCsv.Split(',')) |
| 54 | + count += s.Length; |
| 55 | + return count; |
| 56 | + } |
| 57 | + |
| 58 | + [BenchmarkCategory("CharSplit-Foreach"), Benchmark(Description = "SplitAsSegments (ZLinq)")] |
| 59 | + public int CharSplit_Foreach_ZLinq() |
| 60 | + { |
| 61 | + int count = 0; |
| 62 | + foreach (var s in SmallCsv.SplitAsSegments(',')) |
| 63 | + count += s.Length; |
| 64 | + return count; |
| 65 | + } |
| 66 | + |
| 67 | + // ===================================================================== |
| 68 | + // CATEGORY: Char Split - With LINQ Count() |
| 69 | + // ===================================================================== |
| 70 | + |
| 71 | + [BenchmarkCategory("CharSplit-Count"), Benchmark(Baseline = true, Description = "BCL Split + LINQ Count")] |
| 72 | + public int CharSplit_Count_BCL() |
| 73 | + { |
| 74 | + return SmallCsv.Split(',').Count(); |
| 75 | + } |
| 76 | + |
| 77 | + [BenchmarkCategory("CharSplit-Count"), Benchmark(Description = "SplitAsSegments + ZLinq Count")] |
| 78 | + public int CharSplit_Count_ZLinq() |
| 79 | + { |
| 80 | + return SmallCsv.SplitAsSegments(',').Count(); |
| 81 | + } |
| 82 | + |
| 83 | + // ===================================================================== |
| 84 | + // CATEGORY: Char Split - Large String Foreach |
| 85 | + // ===================================================================== |
| 86 | + |
| 87 | + [BenchmarkCategory("CharSplit-Large"), Benchmark(Baseline = true, Description = "BCL Split (1000 items)")] |
| 88 | + public int CharSplit_Large_BCL() |
| 89 | + { |
| 90 | + int count = 0; |
| 91 | + foreach (var s in LargeCsv.Split(',')) |
| 92 | + count++; |
| 93 | + return count; |
| 94 | + } |
| 95 | + |
| 96 | + [BenchmarkCategory("CharSplit-Large"), Benchmark(Description = "SplitAsSegments (1000 items)")] |
| 97 | + public int CharSplit_Large_ZLinq() |
| 98 | + { |
| 99 | + int count = 0; |
| 100 | + foreach (var s in LargeCsv.SplitAsSegments(',')) |
| 101 | + count++; |
| 102 | + return count; |
| 103 | + } |
| 104 | + |
| 105 | + // ===================================================================== |
| 106 | + // CATEGORY: String Sequence Split |
| 107 | + // ===================================================================== |
| 108 | + |
| 109 | + [BenchmarkCategory("SeqSplit"), Benchmark(Baseline = true, Description = "BCL Split(string)")] |
| 110 | + public int SeqSplit_BCL() |
| 111 | + { |
| 112 | + int count = 0; |
| 113 | + foreach (var s in MediumCsv.Split(",")) |
| 114 | + count += s.Length; |
| 115 | + return count; |
| 116 | + } |
| 117 | + |
| 118 | + [BenchmarkCategory("SeqSplit"), Benchmark(Description = "SplitAsSegments(string)")] |
| 119 | + public int SeqSplit_ZLinq() |
| 120 | + { |
| 121 | + int count = 0; |
| 122 | + foreach (var s in MediumCsv.SplitAsSegments(",")) |
| 123 | + count += s.Length; |
| 124 | + return count; |
| 125 | + } |
| 126 | + |
| 127 | + // ===================================================================== |
| 128 | + // CATEGORY: Regex Split |
| 129 | + // ===================================================================== |
| 130 | + |
| 131 | + [BenchmarkCategory("RegexSplit"), Benchmark(Baseline = true, Description = "Regex.Split")] |
| 132 | + public int RegexSplit_BCL() |
| 133 | + { |
| 134 | + int count = 0; |
| 135 | + foreach (var s in CommaRegex.Split(MediumCsv)) |
| 136 | + count += s.Length; |
| 137 | + return count; |
| 138 | + } |
| 139 | + |
| 140 | + [BenchmarkCategory("RegexSplit"), Benchmark(Description = "SplitAsSegments(Regex)")] |
| 141 | + public int RegexSplit_ZLinq() |
| 142 | + { |
| 143 | + int count = 0; |
| 144 | + foreach (var s in MediumCsv.SplitAsSegments(CommaRegex)) |
| 145 | + count += s.Length; |
| 146 | + return count; |
| 147 | + } |
| 148 | + |
| 149 | + // ===================================================================== |
| 150 | + // CATEGORY: Regex Match Enumeration |
| 151 | + // ===================================================================== |
| 152 | + |
| 153 | + [BenchmarkCategory("RegexMatch"), Benchmark(Baseline = true, Description = "Regex.Matches")] |
| 154 | + public int RegexMatch_BCL() |
| 155 | + { |
| 156 | + int count = 0; |
| 157 | + foreach (Match m in WordRegex.Matches(MediumCsv)) |
| 158 | + count += m.Length; |
| 159 | + return count; |
| 160 | + } |
| 161 | + |
| 162 | + [BenchmarkCategory("RegexMatch"), Benchmark(Description = "AsSegments(Regex)")] |
| 163 | + public int RegexMatch_ZLinq() |
| 164 | + { |
| 165 | + int count = 0; |
| 166 | + foreach (var s in WordRegex.AsSegments(MediumCsv)) |
| 167 | + count += s.Length; |
| 168 | + return count; |
| 169 | + } |
| 170 | + |
| 171 | + // ===================================================================== |
| 172 | + // CATEGORY: Replace Operation |
| 173 | + // ===================================================================== |
| 174 | + |
| 175 | + [BenchmarkCategory("Replace"), Benchmark(Baseline = true, Description = "BCL String.Replace")] |
| 176 | + public string Replace_BCL() |
| 177 | + { |
| 178 | + return MediumCsv.Replace(",", " | "); |
| 179 | + } |
| 180 | + |
| 181 | + [BenchmarkCategory("Replace"), Benchmark(Description = "ReplaceToString (ZLinq)")] |
| 182 | + public string Replace_ZLinq() |
| 183 | + { |
| 184 | + return MediumCsv.AsSegment().ReplaceToString(",", " | "); |
| 185 | + } |
| 186 | + |
| 187 | + // ===================================================================== |
| 188 | + // CATEGORY: LINQ Chain - Where + Select + Count |
| 189 | + // ===================================================================== |
| 190 | + |
| 191 | + [BenchmarkCategory("LinqChain"), Benchmark(Baseline = true, Description = "BCL + LINQ Chain")] |
| 192 | + public int LinqChain_BCL() |
| 193 | + { |
| 194 | + return SmallCsv.Split(',') |
| 195 | + .Where(s => s.Length > 4) |
| 196 | + .Select(s => s.Length) |
| 197 | + .Sum(); |
| 198 | + } |
| 199 | + |
| 200 | + [BenchmarkCategory("LinqChain"), Benchmark(Description = "ZLinq Chain")] |
| 201 | + public int LinqChain_ZLinq() |
| 202 | + { |
| 203 | + return SmallCsv.SplitAsSegments(',') |
| 204 | + .Where(s => s.Length > 4) |
| 205 | + .Select(s => s.Length) |
| 206 | + .Sum(); |
| 207 | + } |
| 208 | + |
| 209 | + // ===================================================================== |
| 210 | + // CATEGORY: ToArray Materialization |
| 211 | + // ===================================================================== |
| 212 | + |
| 213 | + [BenchmarkCategory("ToArray"), Benchmark(Baseline = true, Description = "BCL Split (already array)")] |
| 214 | + public int ToArray_BCL() |
| 215 | + { |
| 216 | + return SmallCsv.Split(',').Length; |
| 217 | + } |
| 218 | + |
| 219 | + [BenchmarkCategory("ToArray"), Benchmark(Description = "SplitAsSegments.ToArray()")] |
| 220 | + public int ToArray_ZLinq() |
| 221 | + { |
| 222 | + return SmallCsv.SplitAsSegments(',').ToArray().Length; |
| 223 | + } |
| 224 | +} |
0 commit comments