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 6094845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:44:58+00:00 2026-05-23T12:44:58+00:00

I have some code that uses deferred execution and lazy loading: public static IEnumerable<XElement>

  • 0

I have some code that uses deferred execution and lazy loading:

    public static IEnumerable<XElement> GetStreamElementP(string fileId, ListProgressEventHandler progressHandler, int total)
    {
        var filePath = Utility.GetEContentFilePath(fileId);
        using (var reader = XmlReader.Create(filePath, new XmlReaderSettings { IgnoreWhitespace = true, }))
        {
            var cnt = 0;
            reader.MoveToContent();
            // Parse the file and display each of the p nodes.
            reader.Read();
            while (reader.NodeType == XmlNodeType.Element && reader.Name == "p")
            {
                cnt++;
                var returnedValue = XElement.ReadFrom(reader) as XElement;

                int rem = cnt % _streamElementCallBackSize;
                if (progressHandler != null && rem == 0)
                {
                    progressHandler(null, new ListProgressEventArgs { ItemsProcessed = cnt, TotalItemsToProcess = total, });
                }
                yield return returnedValue;
            }
            reader.Close();
        }

    }

I’m looking to get a simple count on the number of elements. The current code we are using is:

    public static int FileElementsCount(string fileId)
    {
        var cnt = 0;
        foreach (XElement e in GetStreamElementP(fileId))
        {
            cnt++;
        }
        return cnt;
    }

Can I improve this to?

    public static int FileElementsCount(string fileId)
    {
        return GetStreamElementP(fileId).Count<XElement>();
    }

Or will this cause more memory to be used when getting the count? We are dealing with very large files in some cases and attempting to keep memory usage to a minimum where possible.

I have tried to find a concrete example that explains how the memory is used in each case without any success.

Thanks in advance for any help.

  • 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-05-23T12:44:59+00:00Added an answer on May 23, 2026 at 12:44 pm

    In your case, both ways of computing the count are will do the same.

    The memory consumption of this function should be proportional the size of the <p> elements. So, if there is lots of small elements, it shouldn’t consume large amounts of memory. If you have relatively few huge elements, this could consume quite a lot of memory, because you’re creating an XElement out of each of them. If this was the case, the memory consumption could be made much smaller by not creating them at all.

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

Sidebar

Related Questions

I have some code that uses a form.submit() to open up another window when
Hi I have some code that uses block RandomAccessFile file = new RandomAccessFile(some file,
I have a form that uses wxpython, one of the objects on the form
I keep on running across code that uses double-checked locking, and I'm still confused
Imagine that I have a view, and as part of a view it render
I have an abstract class called DatabaseRow that, after being derived and constructed, is
I have a script that, inserts into the database e.g. 20,000 users with email
I am creating a program that uses JFrame, JPanel, JLabel and all other sorts
I've found some limited use in chaining class functions, say $class->setUser('foo')->getInfo() (bad example) although
Let's say I have something like this: <?php namespace Twitter; class Twitter { function

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.