29 lines
904 B
C#
29 lines
904 B
C#
using System.Text.RegularExpressions;
|
|
using BenchmarkDotNet.Attributes;
|
|
|
|
namespace Benchmark
|
|
{
|
|
[MemoryDiagnoser]
|
|
public class IdentityParser
|
|
{
|
|
private const string TestIdentity = "ABZ0000001-1";
|
|
private readonly Regex _regex = new Regex(@"^(\w{3,})(\d{7})-(\d+)$", RegexOptions.Compiled);
|
|
|
|
[Benchmark]
|
|
public Identity? Parse1()
|
|
{
|
|
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? Parse2()
|
|
{
|
|
return Identity.TryParse(TestIdentity, out var identity) ? identity : default(Identity?);
|
|
}
|
|
}
|
|
} |