Skip to content

Instantly share code, notes, and snippets.

@Brom95
Created September 19, 2022 18:57
Show Gist options
  • Save Brom95/576dec1747fde6d77e7a0d807253df17 to your computer and use it in GitHub Desktop.
Save Brom95/576dec1747fde6d77e7a0d807253df17 to your computer and use it in GitHub Desktop.
Span split benchmark
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace SpanSplit;
public class SplitTest
{
public static string input = "asasdfhsjkdfsfh asdl;fkjasldfjasdfasdfshadjfaskldhfasdwewehfefhhfsjkdh";
[Benchmark]
public string[] SimpleSplit()
{
return input.Split();
}
[Benchmark]
public string[] LimitSplit()
{
return input.Split(" ", 2);
}
[Benchmark]
public string[] SpanSplit()
{
var span = input.AsSpan();
var elementIndex = span.IndexOf(" ");
return new[]
{
span.Slice(0, elementIndex).ToString(),
span.Slice(elementIndex + 1, span.Length - 1 - elementIndex).ToString()
};
}
}
public class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run(typeof(Program).Assembly);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment