I'm unable to determine whether having a growing and shrinking List vs using a big bool Array will be more efficient for my application.
To expand on this comparison and the actual situation, here are examples of each option I believe I have:
Option 1 (List):
public List<int> list = new List<int>();
while (true) { // game loop
list.Add(Random.Range(0-300));
list.Add(Random.Range(0-300));
... // maximum of 10 of these can happen
if (list.Contains(42)) { // roughly 10 - 50 of these checks can be true
list.Remove(42);
}
}
Option 2 (Array):
bool[] arr = new bool[300];
while (true) { // game loop
arr[Random.Range(0-300)] = true;
arr[Random.Range(0-300)] = true;
... // maximum of 10 of these can happen
for (int i = 0; i < 300; i++) {
if (arr[i]) { // roughly 10 - 50 of these checks can be true
arr[i] = false;
}
}
}
So essentially my question is:
At what point does too many .Contains
checks become more expensive than a for
loop over each possible element (based on my ranges)?
IMPORTANT
This is not a List vs Array question. The datatypes are important because of the condition checks. So it is specifically an integer list vs bool array comparison since these two options can give me the same results.