Skip to content

Commit 1d65896

Browse files
committed
Refactor Compare functions as Equal or NotEqual
1 parent 4d88652 commit 1d65896

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- New editor mesh debugger: Window > Analysis > Mesh Debugger
1515
- Behavior help URLs
1616

17+
### Changed
18+
19+
- Refactored the `Compare<T>` class
20+
- Changed the `Compare.Test` function to two distinct functions: `Compare.Equal` and `Compare.NotEqual`
21+
- The generics are now applied to the functions instead of the class, i.e., `Compare.Equal<int>` instead of `Compare<int>.Equal`
22+
1723
## [1.4.0] - 2022/05/20
1824

1925
### Added

Documentation~/articles/benchmarking.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ Benchmark.Run(1000, Foo, Bar, Baz);
2323

2424
## 🎏 Compare Equality
2525

26-
Alternatively, sometimes it is useful to test if the results of two functions are equal. The **Debug Tools** package comes with another static class [Compare](/api/Zigurous.Debug/Compare-1) to handle these tests. The class uses generics to know the type of value you are looking to compare, and it returns the percentage of results that are equal.
26+
Sometimes it is useful to test if multiple functions return the same results. The **Debug Tools** package comes with another static class [Compare](/api/Zigurous.Debug/Compare-1) to handle these tests. The class uses generics to know the type of value you are looking to compare, and it returns the percentage of results that are equal (or not equal) for a given amount of iterations.
2727

2828
```csharp
29-
Compare<bool>.Test(Foo, Bar, 1000);
30-
Compare<float>.Test(Foo, Bar, 1000);
29+
Compare.Equal(Foo, Bar, 1000);
30+
Compare.NotEqual(Foo, Bar, 1000);
3131
```

Runtime/Compare.cs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
namespace Zigurous.Debug
44
{
55
/// <summary>
6-
/// Compares how many results are equal between two functions.
6+
/// Compares the results of multiple functions for equality.
77
/// </summary>
8-
/// <typeparam name="T">The type of value to compare.</typeparam>
9-
public static class Compare<T> where T : IEquatable<T>
8+
public static class Compare
109
{
1110
/// <summary>
12-
/// Tests the equality of the results of two functions with a given
11+
/// Compares how many results of the two functions are equal for a given
1312
/// amount of iterations.
1413
/// </summary>
1514
/// <param name="foo">The first function to execute.</param>
@@ -18,7 +17,8 @@ public static class Compare<T> where T : IEquatable<T>
1817
/// <param name="log">Logs the final comparison result.</param>
1918
/// <param name="logIndividual">Logs the result of each iteration of the functions.</param>
2019
/// <returns>The percentage of equal results.</returns>
21-
public static float Test(Func<T> foo, Func<T> bar, int iterations, bool log = true, bool logIndividual = false)
20+
public static float Equal<T>(Func<T> foo, Func<T> bar, int iterations, bool log = true, bool logIndividual = false)
21+
where T : IEquatable<T>
2222
{
2323
#if UNITY_EDITOR || DEVELOPMENT_BUILD
2424
int amountEqual = 0;
@@ -51,6 +51,50 @@ public static float Test(Func<T> foo, Func<T> bar, int iterations, bool log = tr
5151
#endif
5252
}
5353

54+
/// <summary>
55+
/// Compares how many results of the two functions are not equal for a
56+
/// given amount of iterations.
57+
/// </summary>
58+
/// <param name="foo">The first function to execute.</param>
59+
/// <param name="bar">The second function to execute.</param>
60+
/// <param name="iterations">The amount of times each function is executed.</param>
61+
/// <param name="log">Logs the final comparison result.</param>
62+
/// <param name="logIndividual">Logs the result of each iteration of the functions.</param>
63+
/// <returns>The percentage of equal results.</returns>
64+
public static float NotEqual<T>(Func<T> foo, Func<T> bar, int iterations, bool log = true, bool logIndividual = false)
65+
where T : IEquatable<T>
66+
{
67+
#if UNITY_EDITOR || DEVELOPMENT_BUILD
68+
int amountNotEqual = 0;
69+
70+
for (int i = 0; i < iterations; i++)
71+
{
72+
T resultFoo = foo();
73+
T resultBar = bar();
74+
75+
bool equal = resultFoo.Equals(resultBar);
76+
77+
if (!equal) {
78+
amountNotEqual++;
79+
}
80+
81+
if (log && logIndividual) {
82+
UnityEngine.Debug.Log($"[Compare] {resultFoo.ToString()} vs {resultBar.ToString()} | {(equal ? "Equal" : "Not Equal")}");
83+
}
84+
}
85+
86+
float percentNotEqual = (float)amountNotEqual / (float)iterations;
87+
88+
if (log) {
89+
UnityEngine.Debug.Log($"[Compare] {amountNotEqual.ToString()}/{iterations.ToString()} ({(percentNotEqual * 100f).ToString()}%) not equal results");
90+
}
91+
92+
return percentNotEqual;
93+
#else
94+
return float.NaN;
95+
#endif
96+
}
97+
5498
}
5599

56100
}

0 commit comments

Comments
 (0)