Created
September 19, 2022 18:57
-
-
Save Brom95/576dec1747fde6d77e7a0d807253df17 to your computer and use it in GitHub Desktop.
Span split benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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