Skip to content

Instantly share code, notes, and snippets.

@nishanc
Created December 12, 2021 13:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nishanc/3202d6d467480d3e2a4eade14567c935 to your computer and use it in GitHub Desktop.
Save nishanc/3202d6d467480d3e2a4eade14567c935 to your computer and use it in GitHub Desktop.
[MemoryDiagnoser]
public class BenchmarkDemo2
{
private static readonly string _dateString = "01 05 1991";
[Benchmark(Baseline = true)]
public DateTime Original()
{
var day = _dateString.Substring(0, 2);
var month = _dateString.Substring(3, 2);
var year = _dateString.Substring(6);
return new DateTime(int.Parse(year),
int.Parse(month),
int.Parse(day));
}
[Benchmark]
public DateTime Span()
{
ReadOnlySpan<char> dateSpan = _dateString;
var day = dateSpan.Slice(0, 2);
var month = dateSpan.Slice(3, 2);
var year = dateSpan.Slice(6);
return new DateTime(int.Parse(year),
int.Parse(month),
int.Parse(day));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment