# Performance Quiz #9 : IList<T> List and array speed

A short and sweet quiz with lots of juicy discussion possibilities:

public int Sum(IList<ushort> indices)

{

int result = 0;

for (int i = 0; i < indices.Count; i++)

result += indices[i];

return result;

}

Considering only the time it takes to do the Sum (i.e. assuming we had already set up the array/list) which gives better performance and why?

// #1

ushort[] tmp = new ushort[500000]; // this doesn't count

Sum(tmp); // this is what we are timing

OR

// #2

List<ushort> tmp = new List<ushort>(500000); // this doesn't count

for (int i = 0; i < 500000; i++) tmp.Add(0); // this doesn't count

Sum(tmp); // this is what we are timing

What say you gentle readers?

(my solution is now posted here)