i have following situation:
I have 2 Lists, one filled with HitObject objects, one filled with ReplayFrames. Structure of the relevant objects below.
public class HitObject
{
public int StartTime { get; set; } = 0;
public int EndTime { get; set; } = 0;
public TimeSpan StartTimeSpan => TimeSpan.FromMilliseconds(StartTime);
public TimeSpan EndTimeSpan => TimeSpan.FromMilliseconds(EndTime);
public TimeSpan TotalTimeSpan => TimeSpan.FromMilliseconds(EndTime - StartTime);
public float DistanceFrom(HitObject otherObject) => Vector2.Distance(Position, otherObject.Position);
}
public class ReplayFrame
{
public int Time { get; set; }
public StandardKeys StandardKeys { get; set; }
}
Using some random testdata that is verified to happen in the "wild", I am trying to find all possible replayFrame objects, or iterators of them, to use for comparisons only. The ReplayFrame itself will not be modified.
Assuming there exists an Beatmap.HitObjects List, as well as a Replay.ReplayFrames List, this is what i am currently doing. For reference, i expect each HitObject to be covered by between 10-25 ReplayFrames, where as there is an approx. total of 10,000 Frames and 500 Objects
var watch = new Stopwatch();
watch.Start();
foreach (var hitobj in Beatmap.HitObjects)
{
var HitWindowEdge = 250; // This is Precalced, the value doesnt matter really.
// Get all frames that could affect this object
// FindAll APPROACH, 38ms in test file
var possibleFrames = Replay.ReplayFrames.FindAll((frame) => { return frame.Time >= hitobj.StartTime - HitWindowEdge && frame.Time <= hitobj.StartTime + HitWindowEdge; });
// LINQ APPROACH, 52ms in test file
//var possibleFrames = (from frame in Replay.ReplayFrames where frame.Time >= hitobj.StartTime - HitWindowEdge && frame.Time <= hitobj.StartTime + HitWindowEdge select frame).ToArray();
//Fancy comparisons with the ReplayFrames go here
}
watch.Stop();
Console.WriteLine("Associating hits took: " + watch.ElapsedMilliseconds + "ms");
My questions are:
- Why does FindAll() seem to be the fastest solution?
- Is there a faster solution for my needs (only further comparison, no actual need for copies of objects)?
- I have tried to replace FindAll() with .Where, but the resulting variable in possibleFrames is null. Is there a reason for this?