Skip to content

Instantly share code, notes, and snippets.

@MisterKidX
Last active November 21, 2022 11:39
Show Gist options
  • Save MisterKidX/5d2e5287c8857798ccb81545a9afd1fd to your computer and use it in GitHub Desktop.
Save MisterKidX/5d2e5287c8857798ccb81545a9afd1fd to your computer and use it in GitHub Desktop.
IEquatable<T> Performance with List<T> and structs
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
int capacity = 10_000_000;
List<Vector3NoEquals> Vector3NoEquals = new(capacity);
List<Vector3WithEquals> Vector3GoodEquals = new(capacity);
List<Vector3WithIEquality> Vector3WithIEquality = new(capacity);
for (int i = 0; i < capacity; i++)
{
Vector3NoEquals.Add(new Vector3NoEquals(i, i, i));
Vector3GoodEquals.Add(new Vector3WithEquals(i, i, i));
Vector3WithIEquality.Add(new Vector3WithIEquality(i, i, i));
}
var index1 = new Vector3NoEquals(capacity / 2, capacity / 2, capacity / 2);
var index2 = new Vector3WithEquals(capacity / 2, capacity / 2, capacity / 2);
var index3 = new Vector3WithIEquality(capacity / 2, capacity / 2, capacity / 2);
Console.WriteLine($"Results for {capacity.ToString("N")} iterations on a list.");
Stopwatch sw = Stopwatch.StartNew();
// -------------------
Vector3NoEquals.Contains(index1);
Console.WriteLine($"Vector 3 without equality override, Contains(miliseconds) {sw.ElapsedMilliseconds}");
sw.Restart();
// --
Vector3GoodEquals.Contains(index2);
Console.WriteLine($"Vector 3 with good equality override, Contains(miliseconds) {sw.ElapsedMilliseconds}");
sw.Restart();
// --
Vector3WithIEquality.Contains(index3);
Console.WriteLine($"Vector 3 with IEquality, Contains(miliseconds) {sw.ElapsedMilliseconds}");
sw.Restart();
struct Vector3WithIEquality : IEquatable<Vector3WithIEquality>
{
public int x;
public int y;
public int z;
public Vector3WithIEquality(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
public bool Equals(Vector3WithIEquality other)
{
return x == other.x && y == other.y && z == other.z;
}
//public override bool Equals(object obj) // Overridden from object
//{
// if (!(obj is Vector3WithIEquality))
// return false;
// Vector3WithIEquality v3 = (Vector3WithIEquality)obj;
// return
// this.x == v3.x &&
// this.y == v3.y &&
// this.z == v3.z;
//}
public override int GetHashCode()
{
return this.x.GetHashCode() ^ this.y.GetHashCode() << 2 ^ this.z.GetHashCode() >> 2;
}
}
struct Vector3NoEquals
{
public int x;
public int y;
public int z;
public Vector3NoEquals(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
public override int GetHashCode()
{
return this.x.GetHashCode() ^ this.y.GetHashCode() << 2 ^ this.z.GetHashCode() >> 2;
}
}
struct Vector3WithEquals
{
public int x;
public int y;
public int z;
public Vector3WithEquals(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
public override bool Equals(object obj) // Overridden from object
{
if (!(obj is Vector3WithEquals))
return false;
Vector3WithEquals v3 = (Vector3WithEquals)obj;
return
this.x == v3.x &&
this.y == v3.y &&
this.z == v3.z;
}
public override int GetHashCode()
{
return this.x.GetHashCode() ^ this.y.GetHashCode() << 2 ^ this.z.GetHashCode() >> 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment