I have a lazy linq query which discovers the number of elements in some xml in memory which contains a particular attribute. I want to assess the length of time it will take to enumerate that query for varying lengths of xml.
In order to do this I first considered using a call to .ToList() as I know that causes an enumeration of all Items. However I decided that this might not represent full evaluation time as there would be a memory operation to move all items from one structure to another and so switched to a call to .Count().
IEnumerable<XMLNode> nodes = "<Some Linq>"
var watch = new Stopwatch();
watch.Restart();
var test = nodes.Count();
watch.Stop();
Console.WriteLine("Enumeration Time: " + watch.ElapsedMilliseconds);
First question would be is this the best way to calculate the time it takes to evaluate that Enumerable?
Second question would be which is more representative of the actual evaluation time Count() or ToList()?
Here are some results from the code, note that between iterations a new portion of xml is added, this means that the xml being searched grows by the same amount each time.
Resuts (from .Count())
Enumeration Time: 0 (ms)
Enumeration Time: 0 (ms)
Enumeration Time: 1 (ms)
Enumeration Time: 1 (ms)
Enumeration Time: 2 (ms)
Enumeration Time: 2 (ms)
Enumeration Time: 2 (ms)
Enumeration Time: 3 (ms)
Enumeration Time: 3 (ms)
Enumeration Time: 4 (ms)
Enumeration Time: 4 (ms)
Enumeration Time: 5 (ms)
Enumeration Time: 6 (ms)
Enumeration Time: 6 (ms)
Enumeration Time: 8 (ms)
Enumeration Time: 6 (ms)
Enumeration Time: 15 (ms)
Enumeration Time: 8 (ms)
Enumeration Time: 8 (ms)
Enumeration Time: 9 (ms)
Enumeration Time: 8 (ms)
Enumeration Time: 9 (ms)
Enumeration Time: 10 (ms)
Enumeration Time: 10 (ms)
Enumeration Time: 10 (ms)
Enumeration Time: 27 (ms)
Enumeration Time: 12 (ms)
Enumeration Time: 18 (ms)
Enumeration Time: 20 (ms)
Resuts (from .ToList())
Enumeration Time: 1 (ms)
Enumeration Time: 1 (ms)
Enumeration Time: 1 (ms)
Enumeration Time: 2 (ms)
Enumeration Time: 2 (ms)
Enumeration Time: 3 (ms)
Enumeration Time: 3 (ms)
Enumeration Time: 4 (ms)
Enumeration Time: 4 (ms)
Enumeration Time: 5 (ms)
Enumeration Time: 5 (ms)
Enumeration Time: 9 (ms)
Enumeration Time: 14 (ms)
Enumeration Time: 12 (ms)
Enumeration Time: 10 (ms)
Enumeration Time: 8 (ms)
Enumeration Time: 9 (ms)
Enumeration Time: 10 (ms)
Enumeration Time: 13 (ms)
Enumeration Time: 12 (ms)
Enumeration Time: 12 (ms)
Enumeration Time: 16 (ms)
Enumeration Time: 21 (ms)
Enumeration Time: 18 (ms)
Enumeration Time: 15 (ms)
Enumeration Time: 15 (ms)
Enumeration Time: 23 (ms)
Enumeration Time: 15 (ms)
Enumeration Time: 38 (ms)
The problem with using
var test = nodes.Count();is that the underlying collection (if there is one) might be implementingIList<T>which has aCountproperty.As an optimization that property could be called and you would get near constant time whatever size the collection.
Instead of
ToList()orCount()consider actually iterating:Note that we are not doing anything with the
item– this will just iterate with minimal overheads.