-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector2.cs
214 lines (196 loc) · 8.04 KB
/
Vector2.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Threading;
namespace Benchmarks
{
public class Vector2
{
public float x;
public float y;
public static readonly Vector2 up = new Vector2(0, 1);
public static readonly Vector2 left = new Vector2(-1, 0);
public static readonly Vector2 right = new Vector2(1, 0);
public static readonly Vector2 down = new Vector2(0, -1);
public Vector2()
{
this.x = 0;
this.y = 0;
}
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
public override string ToString()
{
return "{x:" + x + " y:" + y + "}";
}
public override int GetHashCode()
{
return x.GetHashCode() ^ (y.GetHashCode() << 2);
}
public override bool Equals(object other)
{
if (!(other is Vector2)) return false;
return Equals((Vector2)other);
}
public bool Equals(Vector2 other)
{
return x == other.x && y == other.y;
}
public static float Distance(Vector2 from, Vector2 to)
{
return MathF.Sqrt(MathF.Pow(Math.Abs(from.x - to.x), 2) + MathF.Pow(Math.Abs(from.y - to.y), 2));
}
private static Vector2 Benchtest1(float signedAngle)
{
return (signedAngle > 0) ? (Math.Abs(signedAngle) < 22.5f) ? Vector2.up : (Math.Abs(signedAngle) < 67.5f) ? new Vector2(-.7f, .7f) : (Math.Abs(signedAngle) < 112.5f) ? Vector2.left : (Math.Abs(signedAngle) < 157.5f) ? new Vector2(-.7f, -.7f) : Vector2.down : (Math.Abs(signedAngle) < 22.5f) ? Vector2.up : (Math.Abs(signedAngle) < 67.5f) ? new Vector2(.7f, .7f) : (Math.Abs(signedAngle) < 112.5f) ? Vector2.right : (Math.Abs(signedAngle) < 157.5f) ? new Vector2(.7f, -.7f) : Vector2.down;
}
private static Vector2 Benchtest2(float signedAngle)
{
Vector2 instPlace;
if (Math.Abs(signedAngle) > 22.5f && Math.Abs(signedAngle) < 67.5f)
instPlace = (signedAngle > 0) ? new Vector2(-.7f, .7f) : new Vector2(.7f, .7f);
else if (Math.Abs(signedAngle) > 67.5f && Math.Abs(signedAngle) < 112.5f)
instPlace = (signedAngle > 0) ? Vector2.left : Vector2.right;
else if (Math.Abs(signedAngle) > 112.5f && Math.Abs(signedAngle) < 157.5f)
instPlace = (signedAngle > 0) ? new Vector2(-.7f, -.7f) : new Vector2(.7f, -.7f);
else
instPlace = (Math.Abs(signedAngle) > 90) ? Vector2.down : Vector2.up;
return instPlace;
}
public static void Benchmark(int threadCount)
{
List<Thread> threads = new List<Thread>();
Random rng = new Random();
Vector2 instPlace;
float signedAngle;
List<BigInteger> ternary = new List<BigInteger>();
List<BigInteger> ifelse = new List<BigInteger>();
Console.WriteLine("How many iterations per type of test? (int)");
BigInteger iterationsPerTest = BigInteger.Parse(Console.ReadLine());
Console.WriteLine("How many total iterations? (int)");
BigInteger iterations = BigInteger.Parse(Console.ReadLine());
Console.WriteLine("Press enter to start");
Console.ReadLine();
BigInteger totalIterations = 0;
Console.WriteLine("Iterations = " + iterations + " Threads = " + threadCount);
List<int> counting = new List<int>();
for (int count = 0; count < threadCount; count++)
{
Thread newThread = new Thread(() =>
{
Console.WriteLine(Thread.CurrentThread.Name + " - Starting");
Thread.Sleep(1500);
Stopwatch s = new Stopwatch();
int totalThreadIterations = 0;
while (counting.Count < iterations)
{
counting.Add(1);
signedAngle = ((float)rng.NextDouble() * 360f) - 180f;
Console.WriteLine("Angle: " + signedAngle);
s.Restart();
for (BigInteger b = 0; b < iterationsPerTest; b++)
{
instPlace = Benchtest1(signedAngle);
totalThreadIterations++;
}
s.Stop();
ternary.Add(s.ElapsedMilliseconds);
Console.WriteLine(s.ElapsedMilliseconds);
s.Restart();
for (BigInteger a = 0; a < iterationsPerTest; a++)
{
instPlace = Benchtest2(signedAngle);
totalThreadIterations++;
}
s.Stop();
ifelse.Add(s.ElapsedMilliseconds);
Console.WriteLine(s.ElapsedMilliseconds);
Console.WriteLine();
}
Console.WriteLine("Inverting");
Console.WriteLine();
Thread.Sleep(1000);
while (counting.Count < iterations*2)
{
counting.Add(1);
signedAngle = ((float)rng.NextDouble() * 360f) - 180f;
Console.WriteLine("Angle: " + signedAngle);
s.Restart();
for (BigInteger b = 0; b < iterationsPerTest; b++)
{
instPlace = Benchtest2(signedAngle);
totalThreadIterations++;
}
s.Stop();
ifelse.Add(s.ElapsedMilliseconds);
Console.WriteLine(s.ElapsedMilliseconds);
s.Restart();
for (BigInteger a = 0; a < iterationsPerTest; a++)
{
instPlace = Benchtest1(signedAngle);
totalThreadIterations++;
}
s.Stop();
ternary.Add(s.ElapsedMilliseconds);
Console.WriteLine(s.ElapsedMilliseconds);
Console.WriteLine();
}
totalIterations += totalThreadIterations;
});
newThread.Name = "Thread " + count;
threads.Add(newThread);
}
foreach (Thread t in threads)
t.Start();
foreach (Thread t in threads)
t.Join();
BigInteger total = 0;
foreach (BigInteger value in ternary)
total += value;
Console.WriteLine("Ternary avg = " + (total / ternary.Count));
total = 0;
foreach (BigInteger value in ifelse)
{
total += value;
}
Console.WriteLine("Ifelse avg = " + (total / ifelse.Count));
Console.WriteLine("total iterations = " + totalIterations);
}
}
public class Vector2Int
{
public int x;
public int y;
public Vector2Int()
{
this.x = 0;
this.y = 0;
}
public Vector2Int(int x, int y)
{
this.x = x;
this.y = y;
}
public override string ToString()
{
return "{x:" + x + " y:" + y + "}";
}
public override int GetHashCode()
{
return x.GetHashCode() ^ (y.GetHashCode() << 2);
}
public override bool Equals(object other)
{
if (!(other is Vector2Int)) return false;
return Equals((Vector2Int)other);
}
public bool Equals(Vector2Int other)
{
return x == other.x && y == other.y;
}
}
}