-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParsingDateTimeBenchmarks.cs
55 lines (46 loc) · 1.25 KB
/
ParsingDateTimeBenchmarks.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using BenchmarkDotNet.Attributes;
namespace Ametrin.Optional.Benchy.Examples;
[MemoryDiagnoser(false)]
public class ParsingDateTimeBenchmarks
{
const string Valid = "09/01/2025 16:16:32";
const string Invalid = "dfhalskdjfhdf";
[Benchmark]
public DateTime Option_Success()
{
return Option.Success(Valid).Parse<DateTime>().Or(DateTime.MaxValue);
}
[Benchmark]
public DateTime Option_Error()
{
return Option.Success(Invalid).Parse<DateTime>().Or(DateTime.MaxValue);
}
[Benchmark]
public DateTime RefOption_Success()
{
return RefOption.Success<ReadOnlySpan<char>>(Valid).Parse<DateTime>().Or(DateTime.MaxValue);
}
[Benchmark]
public DateTime RefOption_Error()
{
return RefOption.Success<ReadOnlySpan<char>>(Invalid).Parse<DateTime>().Or(DateTime.MaxValue);
}
[Benchmark]
public DateTime Default_Success()
{
if(DateTime.TryParse(Valid, out var result))
{
return result;
}
return DateTime.MaxValue;
}
[Benchmark]
public DateTime Default_Error()
{
if(DateTime.TryParse(Invalid, out var result))
{
return result;
}
return DateTime.MaxValue;
}
}