Skip to content

Commit

Permalink
removed some benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfKoban committed Dec 8, 2024
1 parent e6495eb commit 654f8eb
Showing 1 changed file with 3 additions and 109 deletions.
112 changes: 3 additions & 109 deletions BenchmarkConsole/Benchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BenchmarkDotNet.Attributes;

Expand All @@ -17,20 +13,8 @@ public class Benchmarks
[Params(100, 125, 150, 175)]
public int Times;

// [Benchmark(Baseline = true)]
public string HumanizedConcatenated_Before()
{
return data.HumanizedConcatenated();
}

// [Benchmark]
public string HumanizedConcatenated_After()
{
return HumanizedConcatenatedNew(data);
}

// [Benchmark(Baseline = true)]
// public void MiKo_2023_Original() => GC.KeepAlive(new MiKoSolutions.Analyzers.Rules.Documentation.MiKo_2023_CodeFixProvider.MapData());
// [Benchmark(Baseline = true)]
// public void MiKo_2023_Original() => GC.KeepAlive(new MiKoSolutions.Analyzers.Rules.Documentation.MiKo_2023_CodeFixProvider.MapData());

// [Benchmark(Baseline = true)]
// [Benchmark]
Expand All @@ -40,98 +24,8 @@ public string HumanizedConcatenated_After()
// [Benchmark]
// public void MiKo_2060_Original() => GC.KeepAlive(new MiKoSolutions.Analyzers.Rules.Documentation.MiKo_2060_CodeFixProvider.MapData());

//[Benchmark(Baseline = true)]
// [Benchmark(Baseline = true)]
// [Benchmark]
// public void MiKo_2080_Original() => GC.KeepAlive(new MiKoSolutions.Analyzers.Rules.Documentation.MiKo_2080_CodeFixProvider.MapData());

[Benchmark]
public void ArrayViaNew()
{
GC.KeepAlive(new int[Times]);
}

[Benchmark]
public void SharedArrayPool()
{
var array = ArrayPool<int>.Shared.Rent(Times);
ArrayPool<int>.Shared.Return(array);
}

private string[] data;

[GlobalSetup]
public void Setup()
{
data = new string[Times];

Array.Fill(data, "a");
}

public static string HumanizedConcatenatedNew(IEnumerable<string> values, string lastSeparator = "or")
{
var items = values.Select(_ => _.SurroundedWithApostrophe()).ToArray();

var count = items.Length;

switch (count)
{
case 0: return string.Empty;
case 1: return items[0];
}

var items0 = items[0];
var items1 = items[1];

return HumanizedConcatenatedCore().ToString();

StringBuilder HumanizedConcatenatedCore()
{
const string Separator = ", ";

switch (count)
{
case 2:
{
var builder = new StringBuilder(items0.Length + items1.Length + lastSeparator.Length + 2);

return builder.Append(items0).Append(' ').Append(lastSeparator).Append(' ').Append(items1);
}

case 3:
{
var items2 = items[2];

var builder = new StringBuilder(items0.Length + items1.Length + items2.Length + Separator.Length + lastSeparator.Length + 2);

return builder.Append(items0).Append(Separator).Append(items1).Append(' ').Append(lastSeparator).Append(' ').Append(items2);
}

case 4:
{
var items2 = items[2];
var items3 = items[3];

var builder = new StringBuilder(items0.Length + items1.Length + items2.Length + items3.Length + Separator.Length + Separator.Length + lastSeparator.Length + 2);

return builder.Append(items0).Append(Separator).Append(items1).Append(Separator).Append(items[2]).Append(' ').Append(lastSeparator).Append(' ').Append(items3);
}

default:
{
var builder = new StringBuilder(128);

var last = count - 1;
var beforeLast = last - 1;

for (var index = 0; index < beforeLast; index++)
{
builder.Append(items[index]).Append(Separator);
}

return builder.Append(items[beforeLast]).Append(' ').Append(lastSeparator).Append(' ').Append(items[last]);
}
}
}
}
}
}

0 comments on commit 654f8eb

Please sign in to comment.