XPathNodeIterator.Count Performance Issues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Volkan

    #1

    XPathNodeIterator.Count Performance Issues

    Hi,

    I'm trying to compare two XML documents and i'm using XPath queries to
    select nodes. XPathNavigator' s Select method runs fast enough and
    returns an XPathNodeIterat or object. When i try to access this
    iterator's Count property the process extremely slows down. If i just
    iterate throgh 18.000 nodes and call XPathNavigator. Select to find
    equivalent node from the other document it doesn't even take 1 second.
    But in the same loop, when i try to access XPathNodeIterat or.Count, (by
    accessing i mean just assigning its value to a variable) it takes about
    5 minutes.

    I tried to use XPathNavigator. Evaluate and used XPath function boolean
    inside the expression. And this reduced the elapsed time down to 3
    minutes but it's still too much for me. (Btw, using XPath's count
    function took longer.)

    Can someone please give me some advise about how to overcome this
    problem? Any input is appreciated.

    Thanks in advance.

    Best regards,

    Volkan

  • Sergey Dubinets

    #2
    Re: XPathNodeIterat or.Count Performance Issues

    XPathNavigator. Select() doesn't evaluate the expression, just parses it.

    XPathNodeIterat or.MoveNext() does actual "lasy" evaluation and positiones
    iterator to next node.
    XPathNodeIterat or.Count() called firs time clones entire expression and
    evaluate it counting nodes. I expect that Clone() is expensive on big
    node-sets.

    I'd like to see the repro: XML file, XPath expression, how you create
    XPathNodeIterat or and how you use it. (sdub.xslt@mail null.com)

    As work around you can try XPath function count(). It may be more efficient
    then XPathNodeIterat or.Count();

    public virtual int Count {
    get {

    if (count == -1) {

    XPathNodeIterat or clone = this.Clone();

    while(clone.Mov eNext()) ;

    count = clone.CurrentPo sition;

    }

    return count;

    }

    }

    Sergey

    "Volkan" <volkan.paksoy@ gmail.comwrote in message
    news:1155060958 .927174.172810@ n13g2000cwa.goo glegroups.com.. .
    Hi,
    >
    I'm trying to compare two XML documents and i'm using XPath queries to
    select nodes. XPathNavigator' s Select method runs fast enough and
    returns an XPathNodeIterat or object. When i try to access this
    iterator's Count property the process extremely slows down. If i just
    iterate throgh 18.000 nodes and call XPathNavigator. Select to find
    equivalent node from the other document it doesn't even take 1 second.
    But in the same loop, when i try to access XPathNodeIterat or.Count, (by
    accessing i mean just assigning its value to a variable) it takes about
    5 minutes.
    >
    I tried to use XPathNavigator. Evaluate and used XPath function boolean
    inside the expression. And this reduced the elapsed time down to 3
    minutes but it's still too much for me. (Btw, using XPath's count
    function took longer.)
    >
    Can someone please give me some advise about how to overcome this
    problem? Any input is appreciated.
    >
    Thanks in advance.
    >
    Best regards,
    >
    Volkan
    >

    Comment

    • Volkan

      #3
      Re: XPathNodeIterat or.Count Performance Issues


      Hi Sergey,

      Thanks you for your reply.

      Below is how i do what i aim to do :

      // Load files to XPathDocuments
      XPathDocument pathDocOld = new XPathDocument(s trOldFilePath);
      XPathDocument pathDocNew = new XPathDocument(s trNewFilePath);

      // Create XPathNavigators
      navOld = pathDocOld.Crea teNavigator();
      navNew = pathDocNew.Crea teNavigator();

      // Select main nodes from both documents
      XPathNodeIterat or iteratorOld =
      navOld.Select("/Message/RestrictionList/*");
      XPathNodeIterat or iteratorNew =
      navNew.Select("/Message/RestrictionList/*");

      // Start looping with new document.
      while (iteratorNew.Mo veNext())
      {
      // For each item find the corresponding one in the old document
      iteratorOldSub =
      navOld.Select/Message/RestrictionList/RestrictedItem[@ItemId=\"" +
      strItemID + "\"]");

      if( iteratorOldSub. Count == 0 ) (1)
      {
      // Couldn't find the item in the old document so mark this one as
      Inserted in the output.
      }

      }

      As i mentioned earlier, it takes 1 sec with code block (1) and 5 mins
      with it.

      Then i tried :


      bool bExists =
      (bool)navOld.Ev aluate("boolean (/Message/RestrictionList/RestrictedItem[@ItemId=\""
      + strItemID + "\"] )");

      if( bExists )
      {

      }

      This works twice faster but still not enough for me. I wonder what am i
      doing wrong.

      So any suggestions to improve the performance?

      Thanks.

      Volkan

      Comment

      Working...