Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7882593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:19:23+00:00 2026-06-03T04:19:23+00:00

I have a lazy linq query which discovers the number of elements in some

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:19:24+00:00Added an answer on June 3, 2026 at 4:19 am

    The problem with using var test = nodes.Count(); is that the underlying collection (if there is one) might be implementing IList<T> which has a Count property.

    As an optimization that property could be called and you would get near constant time whatever size the collection.

    Instead of ToList() or Count() consider actually iterating:

    foreach(var item in nodes){}
    

    Note that we are not doing anything with the item – this will just iterate with minimal overheads.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already have code which lazy loads scripts on request. My issue now is
I have a situation where lazy-loading is not occurring when I want it to
I have this code: class LFSeq: # lazy infinite sequence with new elements from
I have some code which pulls records out of the database and I'm then
I have the following linq select query that loops round all the 'Places' linked
I have a pretty complex domain entities. I want to have lazy loading for
I have a linq query and I am trying to put that in to
I'm hand-coding Linq to SQL Entities and have a large XML column that I
I have a method GetOrder(int OrderID) that runs a LINQ query and returns an
I'm working with LINQ to objects and have a function where in some cases

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.