-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathRandomWithExclusions.cs
158 lines (147 loc) · 4.21 KB
/
RandomWithExclusions.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
namespace Towel_Benchmarking;
[Tag(Program.Name, "Random With Exclusions")]
[Tag(Program.OutputFile, nameof(RandomWithExclusionsBenchmarks))]
public class RandomWithExclusionsBenchmarks
{
[Params(0)]
public int MinValue;
[Params(10, 100, 1000, 10000, 100000, 1000000)]
public int MaxValue;
[Params("1: sqrt(sqrt(range))", "2: .5*sqrt(range)", "3: sqrt(range)", "4: 2*sqrt(range)")]
public string? Count;
[Params("1: sqrt(sqrt(range))", "2: .5*sqrt(range)", "3: sqrt(range)", "4: 2*sqrt(range)")]
public string? Exclued;
internal Random? Random;
internal int[]? Excludes;
internal int CountInt;
internal int[]? temp;
[IterationSetup]
public void IterationSetup()
{
Random = new Random(7);
int excludeCount = Exclued switch
{
"1: sqrt(sqrt(range))" => (int)Math.Sqrt(Math.Sqrt(MaxValue - MinValue)),
"2: .5*sqrt(range)" => (int)(.5 * Math.Sqrt(MaxValue - MinValue)),
"3: sqrt(range)" => (int)Math.Sqrt(MaxValue - MinValue),
"4: 2*sqrt(range)" => (int)(2 * Math.Sqrt(MaxValue - MinValue)),
_ => throw new NotImplementedException(),
};
Excludes = Random.NextUnique(excludeCount, MinValue, MaxValue);
CountInt = Count switch
{
"1: sqrt(sqrt(range))" => (int)Math.Sqrt(Math.Sqrt(MaxValue - MinValue)),
"2: .5*sqrt(range)" => (int)(.5 * Math.Sqrt(MaxValue - MinValue)),
"3: sqrt(range)" => (int)Math.Sqrt(MaxValue - MinValue),
"4: 2*sqrt(range)" => (int)(2 * Math.Sqrt(MaxValue - MinValue)),
_ => throw new NotImplementedException(),
};
}
[IterationCleanup]
public void IterationCleanUp()
{
// use "temp" to prevent some optimizations... still need to validate IL though
Console.WriteLine(temp!.Sum());
}
[Benchmark]
public void Towel()
{
int[] result = Random!.Next(CountInt, MinValue, MaxValue, Excludes);
temp = result;
}
[Benchmark]
public void HashSetAndArray()
{
System.Collections.Generic.HashSet<int> excluded = new(Excludes!.Length);
foreach (int exclude in Excludes)
{
if (MinValue <= exclude && exclude < MaxValue)
{
excluded.Add(exclude);
}
}
int[] pool = new int[MaxValue - MinValue - excluded.Count];
for (int value = MinValue, i = 0; value < MaxValue; value++)
{
if (!excluded.Contains(value))
{
pool[i++] = value;
}
}
int[] result = new int[CountInt];
for (int i = 0; i < CountInt; i++)
{
int index = Random!.Next(0, pool.Length);
result[i] = pool[index];
}
temp = result;
}
[Benchmark]
public void SetHashLinkedAndArray()
{
Towel.DataStructures.SetHashLinked<int, Int32Equate, Int32Hash> excluded = new(default, default, Excludes!.Length);
foreach (int exclude in Excludes)
{
if (MinValue <= exclude && exclude < MaxValue)
{
excluded.TryAdd(exclude);
}
}
int[] pool = new int[MaxValue - MinValue - excluded.Count];
for (int value = MinValue, i = 0; value < MaxValue; value++)
{
if (!excluded.Contains(value))
{
pool[i++] = value;
}
}
int[] result = new int[CountInt];
for (int i = 0; i < CountInt; i++)
{
int index = Random!.Next(0, pool.Length);
result[i] = pool[index];
}
temp = result;
}
#if false
[Benchmark]
public void SetHashArrayAndArray()
{
Towel.DataStructures.SetHashArray<int, IntEquate, IntHash> excluded = new(default, default, Excludes.Length);
foreach (int exclude in Excludes)
{
if (MinValue <= exclude && exclude < MaxValue)
{
excluded.Add(exclude);
}
}
int[] pool = new int[MaxValue - MinValue - excluded.Count];
for (int value = MinValue, i = 0; value < MaxValue; value++)
{
if (!excluded.Contains(value))
{
pool[i++] = value;
}
}
int[] result = new int[Count];
for (int i = 0; i < Count; i++)
{
int index = Random.Next(0, pool.Length);
result[i] = pool[index];
}
temp = result;
}
#endif
[Benchmark]
public void RelativelySimpleCode()
{
if (MaxValue > 1000)
{
throw new Exception("Slow as balls...");
}
System.Collections.Generic.HashSet<int> exclude = new(Excludes!);
System.Collections.Generic.IEnumerable<int> range = Enumerable.Range(MinValue, MaxValue).Where(i => !exclude.Contains(i));
int[] result = Enumerable.Range(0, CountInt).Select(i => range.ElementAt(Random!.Next(range.Count()))).ToArray();
temp = result;
}
}