Last active
March 16, 2018 12:50
-
-
Save anuviswan/0e00db88e55a47e76d1a4865ca28713f to your computer and use it in GitHub Desktop.
This class benchmarks usage of Span instead of string when parsing sub-string. This is a demonstrative class used in the blog post at www.bytelanguage.net.
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
public class Program | |
{ | |
static void Main() | |
{ | |
BenchmarkRunner.Run<BenchmarkDemo>(); | |
} | |
} | |
[MemoryDiagnoser] | |
public class BenchmarkDemo | |
{ | |
[Params(10,100,400)] | |
public int IterationLimit { get; set; } | |
[Benchmark] | |
public void UsingSubString() | |
{ | |
string keyString = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; | |
for (int i = 0; i < IterationLimit; i++) | |
DummyStringMethod(keyString.Substring(0, i)); | |
} | |
[Benchmark] | |
public void UsingSpan() | |
{ | |
ReadOnlySpan<char> keyString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.".AsReadOnlySpan(); | |
for (int i = 0; i < IterationLimit; i++) | |
DummySpanMethod(keyString.Slice(0, i)); | |
} | |
void DummyStringMethod(string _) { } | |
void DummySpanMethod(ReadOnlySpan<char> _) { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment