ModernUO/Projects/Benchmarks/BenchmarkUtilities/BenchmarkStringHelpers.cs
Kamron Batman 77ce2e1980
fix(core): Optimizes strings / .NET 5 compatibility changes (#354)
- [X] Removes some string allocations (e.g. split)
- [X] Optimizes some collections
- [X] Converts insensitive to extension methods of built-ins.
- [X] Adds ordinal (case sensitive) string helpers
- [X] Fixes conditionals for in-game commands so they use Ordinal comparisons.
- [X] Replaces ToLower.Contains with InsensitiveContains
- [X] Adds ValueStringBuilder
- [X] Implements ValueStringBuilder in a few places where it makes sense
- [X] Removes the redundant Wrap function and replaces it with an optimized version
- [X] Fixes list conversions in Utility

Closes #351

Bumps release version
2020-12-20 23:21:55 -08:00

87 lines
2.2 KiB
C#

using System.Buffers;
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using Server.Buffers;
namespace Benchmarks.BenchmarkUtilities
{
[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.NetCoreApp50)]
public class BenchmarkStringHelpers
{
private readonly string[] names =
{
"Kamron", "Owyn", "Luthius", "Jaedan", "Vorspire", "other people",
"Kamron-2", "Owyn-2", "Luthius-2", "Jaedan-2", "Vorspire-2", "other people too"
};
private int length;
[GlobalSetup]
public void Setup()
{
var chrs = ArrayPool<char>.Shared.Rent(65535);
ArrayPool<char>.Shared.Return(chrs);
length = 0;
for (int i = 0; i < names.Length; i++)
{
length += names.Length;
}
length += 2 * (names.Length - 1) + 3;
}
[Benchmark]
public string BenchmarkStringBuilder()
{
var sb = new StringBuilder();
for (var i = 0; i < names.Length; i++)
{
if (i > 0)
{
sb.Append(i == names.Length - 1 ? ", and" : ", ");
}
sb.Append(names[i]);
}
return sb.ToString();
}
[Benchmark]
public string BenchmarkValueStringBuilderWithStack()
{
using var sb = new ValueStringBuilder(stackalloc char[length]);
for (var i = 0; i < names.Length; i++)
{
if (i > 0)
{
sb.Append(i == names.Length - 1 ? ", and" : ", ");
}
sb.Append(names[i]);
}
return sb.ToString();
}
[Benchmark]
public string BenchmarkValueStringBuilderWithRentedBuffer()
{
using var sb = new ValueStringBuilder(stackalloc char[32]);
for (var i = 0; i < names.Length; i++)
{
if (i > 0)
{
sb.Append(i == names.Length - 1 ? ", and" : ", ");
}
sb.Append(names[i]);
}
return sb.ToString();
}
}
}