-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathProgram.cs
120 lines (94 loc) · 3.4 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System.Reflection;
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<Benchmarks>();
[SimpleJob(RuntimeMoniker.Net70, baseline: true)]
[SimpleJob(RuntimeMoniker.Net80)]
[SimpleJob(RuntimeMoniker.Net60)]
[HideColumns(Column.Job, Column.RatioSD)]
public class Benchmarks
{
private readonly List<int> _numbers = Enumerable.Range(0, 10000).Select(s => Random.Shared.Next()).ToList();
private readonly HashSet<int> _hashSet = Enumerable.Range(0, 10000).ToHashSet();
private readonly Dictionary<int, int> _dictionary = Enumerable.Range(0, 10000).ToDictionary(k => k, v => v);
private const string Text1 = "Hello woRld! ThIs is a test.It CoUlD be a lot longer, but it'S not. Or is it?";
private const string Text2 = "Hello world! This is a test.it could be a lot longer, but it's not. Or is it?";
[Benchmark]
public string ReplaceStringBuilder() => new StringBuilder(Text1).Replace("Hello", "Goodbye").ToString();
[Benchmark]
public bool EqualsOrdinalIgnoreCase() => Text1.Equals(Text2, StringComparison.OrdinalIgnoreCase);
[Benchmark]
public bool EqualsOrdinal() => Text1.Equals(Text2, StringComparison.Ordinal);
[Benchmark]
public bool ContainsOrdinalIgnoreCase() => Text1.Contains("Or is it?", StringComparison.OrdinalIgnoreCase);
[Benchmark]
public bool ContainsOrdinal() => Text1.Contains("Or is it?", StringComparison.Ordinal);
[Benchmark]
public bool StartsWithOrdinalIgnoreCase() => Text1.StartsWith("Hello World", StringComparison.OrdinalIgnoreCase);
[Benchmark]
public bool StartsWithOrdinal() => Text1.StartsWith("Hello World", StringComparison.Ordinal);
[Benchmark]
public int LinqMin() => _numbers.Min();
[Benchmark]
public int LinqMax() => _numbers.Max();
[Benchmark]
public int ReflectionCreateInstance() => (int)Activator.CreateInstance(typeof(int));
[Benchmark]
public string InvokeMethodViaReflection()
{
var method = typeof(string).GetMethod("ToLowerInvariant", BindingFlags.Public | BindingFlags.Instance);
return (string)method.Invoke(Text1, null);
}
[Benchmark]
public DayOfWeek EnumParse() => Enum.Parse<DayOfWeek>("Saturday");
[Benchmark]
public DayOfWeek[] EnumGetValues() => Enum.GetValues<DayOfWeek>();
[Benchmark]
public string[] EnumGetNames() => Enum.GetNames<DayOfWeek>();
[Benchmark]
public List<int> ListAdd10000()
{
var list = new List<int>();
for (var i = 0; i < 10_000; i++)
{
list.Add(i);
}
return list;
}
[Benchmark]
public int HashSetLookup()
{
var entriesFound = 0;
for (var i = 0; i < 10000; i++)
{
if (_hashSet.Contains(i))
entriesFound++;
}
return entriesFound;
}
[Benchmark]
public int DictionaryKeyLookup()
{
var entriesFound = 0;
for (var i = 0; i < 10000; i++)
{
if (_dictionary.ContainsKey(i))
entriesFound++;
}
return entriesFound;
}
[Benchmark]
public int DictionaryValueLookup()
{
var entriesFound = 0;
for (var i = 0; i < 10000; i++)
{
if (_dictionary.ContainsValue(i))
entriesFound++;
}
return entriesFound;
}
}