-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
Program.cs
51 lines (46 loc) · 1 KB
/
Program.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
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Text;
[MemoryDiagnoser]
public class BenchMe
{
private readonly int year;
[Params(1900, 2000, 2019, 2020)]
public int testYear;
public BenchMe()
{
year = testYear;
}
[Benchmark]
public bool IsLeapYearChain()
{
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
[Benchmark]
public bool IsLeapYearTernary()
{
return year % 100 == 0 ? year % 400 == 0 : year % 4 == 0;
}
[Benchmark]
public bool IsLeapYearSwitch()
{
return (year % 4, year % 100, year % 400) switch
{
(_, 0, 0) => true,
(_, 0, _) => false,
(0, _, _) => true,
_ => false,
};
}
}
static class Program
{
public static void Main()
{
var summary = BenchmarkRunner.Run<BenchMe>();
}
}