I have a line like the following in my code:
potentialCollisionsX.Intersect(potentialCollisionsY).Distinct().ToList();
Which, through profiling, i have determined that it is eating approximately 56 percent of my time. I need to figure out how to provide a efficient implementation. I tried
List<Extent> probableCollisions = new List<Extent>();
for (int j = 0; j < potentialCollisionsX.Count; j++)
{
if (potentialCollisionsY.Contains(potentialCollisionsX[j]) && !probableCollisions.Contains(potentialCollisionsX[j]))
{
probableCollisions.Add(potentialCollisionsX[j]);
}
}
but that only drops it to 42 percent. Optimizations or alternative ideas would be much appreciated.
Edit: Someone requested information about the Extent class, and i can't think of a better way to give them information than providing the class definition.
private enum ExtentType { Start, End }
private sealed class Extent
{
private ExtentType _type;
public ExtentType Type
{
get
{
return _type;
}
set
{
_type = value;
_hashcode = 23;
_hashcode *= 17 + Nucleus.GetHashCode();
}
}
private Nucleus _nucleus; //Nucleus is the main body class in my engine
public Nucleus Nucleus
{
get
{
return _nucleus;
}
set
{
_nucleus = value;
_hashcode = 23;
_hashcode *= 17 + Nucleus.GetHashCode();
}
}
private int _hashcode;
public Extent(Nucleus nucleus, ExtentType type)
{
Nucleus = nucleus;
Type = type;
_hashcode = 23;
_hashcode *= 17 + Nucleus.GetHashCode();
}
public override bool Equals(object obj)
{
return Equals(obj as Extent);
}
public bool Equals(Extent extent)
{
if (this.Nucleus == extent.Nucleus) //nucleus.Equals does an int comparison
{
return true;
}
return false;
}
public override int GetHashCode()
{
return _hashcode;
}
}
Edit2: It would seem that using hashsets makes this part of my code as performant as i need, so thanks for your guy's help!