In the code below, which method will be more performant as memory usage and CPU time? I tested performance, recursion method method is better for CPU time but couldn’t test memory usage (I don’t use recursion method like this, it will cause stack overflow exception, just wondering).
private List<int> ints = new List<int>();
void Start()
{
for (int i = 0; i < 9999; i++)
{
ints.Add(i);
}
RecursionMethod(); // this
WhileLoop(); // or this?
}
public void RecursionMethod()
{
if (ints.Count == 0)
{
return;
}
ints.RemoveAt(ints.Count - 1);
RecursionMethod();
}
public void WhileLoop()
{
while (ints.Count != 0)
{
ints.RemoveAt(ints.Count - 1);
}
}
5