[xml] Simple Element+Attribute Lookup with XPath - performance ?
- From: Martin Trappel <0xCDCDCDCD gmx at>
- To: xml gnome org
- Subject: [xml] Simple Element+Attribute Lookup with XPath - performance ?
- Date: Wed, 16 Jul 2008 07:40:21 +0200
-------- Original Message --------
Subject: Simple Element+Attribute Lookup with XPath - performance ?
Date: Tue, 15 Jul 2008 16:16:18 +0200
From: 0xCDCDCDCD gmx at
To: xml gnome org
Hi.
Given a document tree I have to find all (direct) children of a certain
element given an attribute value.
<ROOT>
<FOO Name="a">
<BAR Id="2"/>
<BAR Id="1"/>
<BAR Id="4"/>
...
<BAR Id="2345"/>
</FOO>
<FOO Name="b">
...
</FOO>
...
</ROOT>
I have an xmlNodePtr pointing to a certain FOO element and now would
like to find a certain BAR element of this FOO (lets say the one with id=4).
Possibility a)
Use the xpath search expression 'BAR[ id="4"]' and use the returned nodeset.
###
// take_start_time();
xmlXPathContext* xpc = xmlXPathNewContext(doc);
xpc->node = my_foo_node;
m_xmlXPathObject* xpo =
xmlXPathEvalExpression("BAR[ id=\"4\"]", xpc);
assert( xpo->nodesetval->nodeNr == 1 );
// take_end_time();
###
b) Use xpath search expression 'BAR' and for-loop over the result set,
reading the attribute with xmlGetProp and collecting the ones where the
id matches.
###
// take_start_time();
xmlXPathContext* xpc = xmlXPathNewContext(doc);
xpc->node = my_foo_node;
m_xmlXPathObject* xpo =
xmlXPathEvalExpression("BAR", xpc);
xmlNodePtr found_node = NULL;
for(int i=0, e=xpo->nodesetval->nodeNr; i<e; ++i) {
xmlChar* p_val = xmlGetProp(xpo->nodesetval->nodeTab[i], "id");
if(p_val && 0==strcmp("4", p_val)) {
found_node = xpo->nodesetval->nodeTab[i];
// Note: Do _not_ break; for loop when found
}
if(p_val) xmlFree(p_val);
}
assert( found_node );
// take_end_time();
###
The second version is about 2-3 times faster than the first version with
the attribute lookup in the XPath expression. (it's factor 5-10 in the
debug version, but I guess that is highly compiler dependant)
Is there an easy explanation for this?
Is my XPath expression somehow to general?
Does XPath have to do significantly more when a predicate is present?
( I'm running this with Visual Studio 2005 / VC8 )
thanks, br,
Martin
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]