In the code below, which method will be more performant as memory usage and CPU time? I tested performance, nested method is better for CPU time but couldn’t test memory usage (I don’t use nested methods 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);
}
NestedMethod(); // this
WhileLoop(); // or this?
}
public void NestedMethod()
{
if (ints.Count == 0)
{
return;
}
ints.RemoveAt(ints.Count - 1);
NestedMethod();
}
public void WhileLoop()
{
while (ints.Count != 0)
{
ints.RemoveAt(ints.Count - 1);
}
}