48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
using System;
|
|
using System.Text.RegularExpressions;
|
|
using BenchmarkDotNet.Attributes;
|
|
using BenchmarkDotNet.Jobs;
|
|
|
|
namespace Benchmark
|
|
{
|
|
[MemoryDiagnoser]
|
|
//[SimpleJob(RuntimeMoniker.Net472, baseline: true)]
|
|
[SimpleJob(RuntimeMoniker.NetCoreApp31)]
|
|
[SimpleJob(RuntimeMoniker.NetCoreApp50)]
|
|
public class IdentityParser
|
|
{
|
|
private const string TestIdentity = "ABZ0000001-1";
|
|
private char[] TestIdentityChars;
|
|
|
|
private readonly Regex _regex = new Regex(@"^(\w{3,})(\d{7})-(\d+)$", RegexOptions.Compiled);
|
|
|
|
[GlobalSetup]
|
|
public void GlobalSetup()
|
|
{
|
|
TestIdentityChars = TestIdentity.ToCharArray();
|
|
}
|
|
|
|
[Benchmark]
|
|
public Identity? ParseWithRegex()
|
|
{
|
|
var match = _regex.Match(TestIdentity);
|
|
if (!match.Success) return default;
|
|
var prefix = match.Groups[1].Value;
|
|
var index = int.Parse(match.Groups[2].Value);
|
|
var revision = int.Parse(match.Groups[2].Value);
|
|
return new Identity(prefix, index, revision);
|
|
}
|
|
|
|
[Benchmark]
|
|
public Identity? ParseWithoutRegex()
|
|
{
|
|
return Identity.TryParse(TestIdentity, out var identity) ? identity : default(Identity?);
|
|
}
|
|
|
|
[Benchmark]
|
|
public Identity? ParseWithSpanT()
|
|
{
|
|
return Identity.TryParse(TestIdentityChars, out var identity) ? identity : default(Identity?);
|
|
}
|
|
}
|
|
} |